ExprTk.js

Node.js bindings for ExprTk

License: ISC npm version Node.js CI Test published packages codecov

ExprTk.js supports both synchronous and asynchronous background execution of thunks precompiled from a string including asynchronous and multithreaded versions of TypedArray.prototype.map and TypedArray.prototype.reduce and a synchronous multi-threaded version of TypedArray.prototype.map.

Its main advantage is that it allows deferring of heavy computation for asynchronous execution in a background thread - something that Node.js/V8 does not allow without the very complex mechanisms of worker_threads.

Even in single-threaded synchronous mode ExprTk.js outperforms the native JS TypedArray.prototype.map running in V8 by a significant margin for all types and array sizes and it comes very close to a direct iterative for loop.

It also supports being directly called from native add-ons, including native threads, without any synchronization with V8, allowing JS code to drive multi-threaded mathematical transformations.

Installation

npm install exprtk.js

Usage

const Float64Expression = require('exprtk.js').Float64;

const expr = new Float64Expression('(a + b) / 2');
const inputArray = new Float64Array(1e6);

// Happens in a background thread, iterates over 'a', with b=4
const outputArray = await expr.mapAsync(inputArray, 'a', { 'b': 4 });

// or with 4 threads on 4 cores
const outputArray = await expr.mapAsync(4, inputArray, 'a', { 'b': 4 });

Refer to the ExprTk manual for the full expression syntax.

Expression

new Expression(expression: string, variables: Array<string>?, vectors: Record<string, number>?): Expression
Parameters
expression (string) function
variables (Array<string>?) An array containing all the scalar variables' names, will be determined automatically if omitted, however order won't be guaranteed, scalars are passed by value
vectors (Record<string, number>?) An object containing all the vector variables' names and their sizes, vector size must be known at compilation (object construction), vectors are passed by reference and can be modified by the ExprTk expression
Returns
Expression: The Expression represents an expression compiled to an AST from a string. Expressions come in different flavors depending on the internal type used.
Example
// This determines the internally used type
const expr = require("exprtk.js").Float64;

// arithmetic mean of 2 variables
const mean = new Expression('(a+b)/2', ['a', 'b']);

// naive stddev of an array of 1024 elements
const stddev = new Expression(
 'var sum := 0; var sumsq := 0; ' + 
 'for (var i := 0; i < x[]; i += 1) { sum += x[i]; sumsq += x[i] * x[i] }; ' +
 '(sumsq - (sum*sum) / x[]) / (x[] - 1);',
 [], {x: 1024})
Static Members
allocator
maxParallel
Instance Members
toString
cwise(threads?, arguments, target?)
eval(arguments)
map(threads?, target?, array, iterator, arguments)
reduce(array, iterator, accumulator, initializer, arguments)
allocator
expression
maxActive
maxParallel
scalars
type
type
vectors