Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
continuation.js
Advanced tools
A module for tail call optimization by Continuation Passing Style (CPS) transformation with trampoline technique for Node.js
A module for tail call optimization by Continuation Passing Style (CPS) transformation with trampoline technique for Node.js
JavaScript is a nice programming language, but compared to Scheme, it doesn't handle tail calls properly. Node.js is often used with callback functions, which tend to be tail calls (but not necessarily recursions).
This module allows to transform native JavaScript code into CPS code in a best effort manner. It utilizes so-called trampoline technique to avoid a stack overflow error. Transforming all function into CPS is not very easy (and sometimes not very efficient), hence it has a fallback mechanism, that is, only supported function style is transformed into CPS and other functions are called in an original style. Because of the fallback mechanism, mixing CPS code and non-CPS code is possible.
Here is the table showing modules that support tail call optimization.
NAME | continuation.js | Brushtail | tailrec.js | thunk.js | tail-call |
---|---|---|---|---|---|
Tail Call Optimization | Yes | Yes | Yes | Yes | Yes |
Mutual TCO | Yes | No | Yes | No | No |
Native JavaScript | Yes | Yes | No | No | Almost |
require() integration | Yes | No | No | No | No |
% git clone https://github.com/dai-shi/continuation.js.git
% cd continuation.js
% ./bin/continuation-compile sample/fact.js > cps_fact.js
% npm install continuation.js
and add the following:
require('continuation.js').enable_on_require();
which transforms all following .js files by require
.
Simple factorial function:
% cat sample/fact.js
function fact(x) {
function fact_tail(x, r) {
if (x === 0) {
return r;
} else {
return fact_tail(x - 1, x * r);
}
}
return fact_tail(x, 1);
}
exports.fact = fact;
% node -e "console.log(require('./sample/fact.js').fact(100000))"
.../continuation.js/sample/fact.js:2
function fact_tail(x, r) {
^
RangeError: Maximum call stack size exceeded
% node -e "require('./lib/continuation.js').enable_on_require();console.log(require('./sample/fact.js').fact(100000))"
Infinity
Mutual recursion example:
% cat sample/mutual.js
function isEven(x) {
if (x === 0) {
return true;
} else {
return isOdd(x - 1);
}
}
function isOdd(x) {
if (x === 0) {
return false;
} else {
return isEven(x - 1);
}
}
exports.isEven = isEven;
exports.isOdd = isOdd;
% node -e "console.log(require('./sample/mutual.js').isOdd(1234567))"
.../sample/mutual.js:1
tion isEven(x) {
^
RangeError: Maximum call stack size exceeded
% node -e "require('./lib/continuation.js').enable_on_require();console.log(require('./sample/mutual.js').isOdd(1234567))"
true
The following is the results of Octane benchmark suites (except for one).
Suite name | Original | CPS transformed | Improved? |
---|---|---|---|
Richards | 324 ops/sec | 38.28 ops/sec | No |
DeltaBlue | 188 ops/sec | 12.90 ops/sec | No |
Encrypt | 162 ops/sec | 122 ops/sec | No |
Decrypt | 8.62 ops/sec | 6.86 ops/sec | No |
RayTrace | 19.71 ops/sec | 4.10 ops/sec | No |
Earley | 278 ops/sec | 33.91 ops/sec | No |
Boyer | 18.19 ops/sec | 1.60 ops/sec | No |
RegExp | 7.12 ops/sec | 5.14 ops/sec | No |
Splay | 123 ops/sec | 75.21 ops/sec | No |
NavierStokes | 2.40 ops/sec | 3.13 ops/sec | Yes |
PdfJS | 2.85 ops/sec | 2.68 ops/sec | No |
Gameboy | 0.98 ops/sec | 0.35 ops/sec | No |
CodeLoadClosure | 370 ops/sec | 372 ops/sec | Yes |
CodeLoadJQuery | 8.83 ops/sec | 10.61 ops/sec | Yes |
Box2D | 2.33 ops/sec | 2.54 ops/sec | Yes |
new Function
is not supported.FAQs
A module for tail call optimization by Continuation Passing Style (CPS) transformation with trampoline technique for Node.js
The npm package continuation.js receives a total of 1 weekly downloads. As such, continuation.js popularity was classified as not popular.
We found that continuation.js demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.