4.4. JavaScript development tips¶
Working with Apache CouchDB’s JavaScript environment is a lot different than working with traditional JavaScript development environments. Here are some tips and tricks that will ease the difficulty.
Check the JavaScript version being used by your CouchDB. As of version 3.2.0, this is reported in the output of
GET /_node/_local/_versions
. Prior to version 3.2.0, you will need to see which JavaScript library is installed by your CouchDB binary distribution, provided by your operating system, or linked by your compilation process.If the version is 1.8.5, this is an old version of JavaScript, only supporting the ECMA-262 5th edition (“ES5”) of the language. ES6/2015 and newer constructs cannot be used.
Fortunately, there are many tools available for transpiling modern JavaScript into code compatible with older JS engines. The Babel Project website, for example, offers an in-browser text editor which transpiles JavaScript in real-time. Configuring CouchDB-compatibility is as easy as enabling the
ENV PRESET
option, and typing “firefox 4.0” into the TARGETS field.The
log()
function will log output to the CouchDB log file or stream. You can log strings, objects, and arrays directly, without first converting to JSON. Use this in conjunction with a local CouchDB instance for best results.Be sure to guard all document accesses to avoid exceptions when fields or subfields are missing:
if (doc && doc.myarray && doc.myarray.length)...
4.5. JavaScript engine versions¶
Until version 3.4 Apache CouchDB used only SpiderMonkey as its underlying JavaScript engine. With version 3.4, it’s possible to configure CouchDB to use QuickJS.
Recent versions of CouchDB may use the node-local _versions
API endpoint to
get the current engine type and version:
% http http://adm:pass@localhost:5984/_node/_local/_versions | jq '.javascript_engine'
{
"version": "1.8.5",
"name": "spidermonkey"
}
4.5.1. SpiderMonkey version compatibility¶
Depending on the CouchDB version and what’s available on supported operating systems, the SpiderMonkey version may be any one of these: 1.8.5, 60, 68, 78, 86 or 91. Sometimes there are differences in supported features between versions. Usually later versions only add features, so views will work on version upgrades. However, there are a few exceptions to this. These are a few known regression or discrepancies between versions:
for each (var x in ...)
Version
1.8.5
supports thefor each (var x in ...)
looping expression. That’s not a standard JavaScript syntax and is not supported in later versions:
% js
js> for each (var x in [1,2]) {print(x)}
1
2
% js91
js> for each (var x in [1,2]) {print(x)}
typein:1:4 SyntaxError: missing ( after for:
typein:1:4 for each (var x in [1,2]) {print(x)}
typein:1:4 ....^
E4X (ECMAScript for XML)
This is not supported in versions greater than
1.8.5
. This feature may be inadvertently triggered when inserting a.
character between a variable and(
. That would compile on1.8.5
and throw aSyntaxError
on other versions:
% js
js> var xml = <root><x></x></root>
js> xml.(x)
<root>
<x/>
</root>
% js91
js> var xml = <root><x></x></root>
typein:1:11 SyntaxError: expected expression, got '<':
typein:1:11 var xml = <root><x></x></root>
typein:1:11 ...........^
toLocaleFormat(...)
function.This
Date
function is not present in versions greater than1.8.5
:
% js
js> d = new Date("Dec 1, 2015 3:22:46 PM")
(new Date(1449001366000))
js> d.toLocaleFormat("%Y-%m-%d")
"2015-12-01"
% js91
js> d = new Date("Dec 1, 2015 3:22:46 PM")
(new Date(1449001366000))
js> d.toLocaleFormat("%Y-%m-%d")
typein:2:3 TypeError: d.toLocaleFormat is not a function
toLocaleString(...)
function.
SpiderMonkey 1.8.5 ignored locale strings. Later versions started to return the correct format:
% js
js > (new Date("2019-01-15T19:32:52.915Z")).toLocaleString('en-US')
"Tue Jan 15 14:32:52 2019"
% js91
js > (new Date("2019-01-15T19:32:52.915Z")).toLocaleString('en-US')
"01/15/2019, 02:32:52 PM"
Spidermonkey 91 output also match QuickJS and v8.
Invalid expressions following
function(){...}
are not ignored any longer and will throw an error.Previously, in versions less than or equal to
1.8.5
it was possible add any expression following the main function definition and they were mostly ignored:
$ http put $DB/db/_design/d4 views:='{"v1":{"map":"function(doc){emit(1,2);} if(x) a"}}'
HTTP/1.1 201 Created
{
"id": "_design/d4",
"ok": true,
"rev": "1-08a7d8b139e52f5f3df5bc27e20eeff1"
}
% http $DB/db/_design/d4/_view/v1
HTTP/1.1 200 OK
{
"offset": 0,
"rows": [
{
"id": "doc1",
"key": 1,
"value": 2
}
],
"total_rows": 1
}
With higher versions of SpiderMonkey, that would throw a compilation error:
$ http put $DB/db/_design/d4 views:='{"v1":{"map":"function(doc){emit(1,2);} if(x) a"}}'
HTTP/1.1 400 Bad Request
{
"error": "compilation_error",
"reason": "Compilation of the map function in the 'v1' view failed: ..."
}
Object key order.
Object key order may change between versions, so any views which rely on that order may emit different results depending on the engine version:
% js
js> r={}; ["Xyz", "abc", 1].forEach(function(v) {r[v]=v;}); Object.keys(r)
["Xyz", "abc", "1"]
% js91
js> r={}; ["Xyz", "abc", 1].forEach(function(v) {r[v]=v;}); Object.keys(r)
["1", "Xyz", "abc"]
String
match(undefined)
Spidermonkey 1.8.5 returns
null
formatch(undefined)
while versions starting with at least78
return[""]
.
% js
js> "abc".match(undefined)
null
% js91
js> "abc".match(undefined)
[""]
4.5.2. Using QuickJS¶
The QuickJS-based JavaScript engine is available as of CouchDB version 3.4. It
has to be explicitly enabled via [couchdb] js_engine = quickjs
and
restarting the service.
Generally, QuickJS engine is a bit faster, consumes less memory, and provides
slightly better isolation between contexts by re-creating the whole javascript
engine runtime on every reset
command.
To try building invidual views using QuickJS, even when the default engine is
SpiderMonkey, can use the "javascript_quickjs"
as the view language,
instead of "javascript"
. Just that view will be rebuilt using the QuickJS
engine. However, when switching back to "javascript"
the view will have to
be re-built again.
4.5.3. QuickJS vs SpiderMonkey incompatibilities¶
The QuickJS engine is quite compatible with SpiderMonkey version 91. The same
incompatibilities between 1.8.5 and 91 are also present between 1.8.5 and
QuickJS. So, when switching from 1.8.5 to QuickJS see the SpiderMonkey version
compatibility
section above.
These are a few incompatibilties between SpiderMonkey 91 and QuickJS engine:
RegExp.$1
, …,RegExp.$9
This is a deprecated JavaScript feature that’s not available in QuickJS. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/n
Date.toString()
doesn’t include the timezone name, just the offset.
% qjs > (new Date()).toString();
"Thu Sep 05 2024 17:03:23 GMT-0400"
% js91
js> (new Date()).toString();
"Thu Sep 05 2024 17:04:03 GMT-0400 (EDT)"
4.5.4. Scanning for QuickJS incompatibilities¶
CouchDB version 3.4 and higher include a background scanner which can be used traverse all the databases and design documents and run them agaiinst SpiderMonkey and the QuickJS engine and report any discrepancies in the logs. That could be a useful run before deciding to switch to QuickJS as the default JavaScript engine.
The scanner can be enabled with:
[couch_scanner_plugins]
couch_quickjs_scanner_plugin = true
And configured to run at a predetermined time or on a periodic schedule. For instance:
[couch_quickjs_scanner_plugin]
after = 2024-09-05T18:10:00
repeat = 1_day
It will not start until after the specified time and then it will run about once every 24 hours.
The logs will indicate when the scan starts and finishes:
couch_quickjs_scanner_plugin s:1725559802-c615220453e6 starting
...
couch_quickjs_scanner_plugin s:1725559802-c615220453e6 completed
During scanning discrepancies are reported in the log. They may look like:
couch_quickjs_scanner_plugin s:1725559802-c615220453e6
db:mydb/40000000-5fffffff
ddoc:_design/mydesign
view validation failed
{map_doc,<<"doc1">>, $quickjs_res, $sm_res}
The s:...
field indicates which scan session it belongs to, which db and
shard range it found the issue on, followed by the design document, and the
document ID. Then, the {map_doc, ..., ...}
tuple indicates which operation
failed (mapping a document) where the 2nd element is the result from the
QuickJS engine, and the 3rd is the result from the SpiderMonkey engine.
Sometimes it maybe needed to ignore some databases or design documents. That
can be done with a number of regular expression patterns in the
[couch_quickjs_scanner_plugin.skip_dbs]
config section:
[couch_quickjs_scanner_plugin.skip_dbs]
pattern1 = bar.*
pattern2 = .*foo