Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
nginx
GitHub Repository: nginx/nginx.org
Path: blob/main/xml/en/docs/njs/preload_objects.xml
1 views
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="3"
13
toc="no">
14
15
<section id="summary">
16
17
<para>
18
<note>
19
Preloaded objects are supported only with the
20
<link doc="engine.xml" id="njs_engine">njs</link> JavaScript engine
21
and are not available with the
22
<link doc="engine.xml" id="quickjs_engine">QuickJS</link> engine.
23
</note>
24
</para>
25
26
<para>
27
For each incoming request njs creates a separate virtual machine.
28
This brings a lot of benefits such as predictable memory consumption
29
or requests isolation.
30
However, as all requests are isolated,
31
if a request handler needs to access some data,
32
it has to read it by itself.
33
This is not efficient especially when the amount of data is large.
34
</para>
35
36
<para>
37
To address this limitation,
38
a preloaded shared object was introduced.
39
Such objects are created immutable and do not have prototype chains:
40
their values cannot be changed, properties cannot be added or removed.
41
</para>
42
43
</section>
44
45
46
<section id="working_with_preload_objects"
47
name="Working with preload objects">
48
49
<para>
50
Here are some examples of how to work with a preload object in njs:
51
52
<list type="bullet">
53
54
<listitem>
55
access properties by name:
56
<programlisting>
57
preloaded_object.prop_name
58
preloaded_object[prop_name]
59
</programlisting>
60
</listitem>
61
62
<listitem>
63
enumerate properties:
64
<programlisting>
65
for (i in preloaded_object_name) {
66
...
67
}
68
</programlisting>
69
</listitem>
70
71
<listitem>
72
apply non-modifying built-in methods using <literal>call()</literal>:
73
<programlisting>
74
Array.prototype.filter.call(preloaded_object_name, ...)
75
</programlisting>
76
</listitem>
77
78
</list>
79
</para>
80
81
</section>
82
83
</article>
84
85