comparison xml/en/docs/njs/preload_objects.xml @ 2984:cc475ba7d406

Added Preload Objects article in njs.
author Yaroslav Zhuravlev <yar@nginx.com>
date Thu, 01 Jun 2023 17:12:18 +0100
parents
children 48f245493600
comparison
equal deleted inserted replaced
2983:67dd348c9757 2984:cc475ba7d406
1 <?xml version="1.0"?>
2
3 <!--
4 Copyright (C) Nginx, Inc.
5 -->
6
7 <!DOCTYPE article SYSTEM "../../../../dtd/article.dtd">
8
9 <article name="Understanding preloaded objects"
10 link="/en/docs/njs/preload_objects.html"
11 lang="en"
12 rev="1"
13 toc="no">
14
15 <section id="summary">
16
17 <para>
18 For each incoming request njs creates a separate virtual machine.
19 This brings a lot of benefits such as predictable memory consumption
20 or requests isolation.
21 </para>
22
23 <para>
24 However, it also has its own downsides.
25 Since all requests are isolated,
26 if a request handler needs to access some data,
27 it has to read it by itself.
28 This is not efficient especially when the amount of data is large.
29 </para>
30
31 <para>
32 To address this limitation,
33 a preloaded shared object was introduced.
34 Such objects are created immutable and do not have prototype chains:
35 their values cannot be changed, properties cannot be added or removed.
36 </para>
37
38 </section>
39
40
41 <section id="working_with_preload_objects"
42 name="Working with preload objects">
43
44 <para>
45 Here are some examples of how to work with a preload object in njs:
46
47 <list type="bullet">
48
49 <listitem>
50 access properties by name:
51 <programlisting>
52 preloaded_object.prop_name
53 preloaded_object[prop_name]
54 </programlisting>
55 </listitem>
56
57 <listitem>
58 enumerate properties:
59 <programlisting>
60 for (i in preloaded_object_name) {
61 ...
62 }
63 </programlisting>
64 </listitem>
65
66 <listitem>
67 apply non-modifying built-in methods using <literal>call()</literal>:
68 <programlisting>
69 Array.prototype.filter.call(preloaded_object_name, ...)
70 </programlisting>
71 </listitem>
72
73 </list>
74 </para>
75
76 </section>
77
78 </article>