mathjs/test/benchmark/expression_parser.js
jhugman e80995d52d
Add support for scopes with get and set methods (#2166)
* Add support for scopes with get and set methods

* Fix build for node v12

* Fixup cli and parser tests

* Add tests for simplify and evaluate

* Add example for a custom scope object

* Function calls need child scopes

* Transitionary step: Separate Safe and Scope Property calls

* Renamed identifiers in FunctionNode

* Evaluate with ObjectScopeWrapper

* Simplify tests passing

* Assume all scopes are map-like. Except parser

* Remove isMapLike check in customs.*SafeProperty() methods

* Change MapLike to Map

* Move keywords from an Object to a Set

* Move ScopeProperty functions in to scope.js

* Removed deprecation warning

* Rename scope.js to map.js

* Rename ScopeProperty to MapProperty

* Add tests and docs for map.js

* Put back the micro-optimization of function calls

* Use Map in the parser

* Called scope methods directly in cli.js

* Coercing of scope into a Map is done in Node, not evaluate

* Move createSubScope to its own file

* Fixup following self-review

* Add scope docs

* Final self-review changes

* Address reviewer comments

* Remove MapProperty witness marks

* Converted broken benchmark possibly lost in a rebase

* Use bare map as scope in benchmark

Co-authored-by: Jos de Jong <wjosdejong@gmail.com>
2021-05-16 13:33:01 +02:00

90 lines
2.5 KiB
JavaScript

// test performance of the expression parser in node.js
// browserify benchmark/expression_parser.js -o ./benchmark_expression_parser.js
const assert = require('assert')
const Benchmark = require('benchmark')
const padRight = require('pad-right')
const math = require('../..')
const { getSafeProperty } = require('../../lib/cjs/utils/customs')
// expose on window when using bundled in a browser
if (typeof window !== 'undefined') {
window.Benchmark = Benchmark
}
function pad (text) {
return padRight(text, 40, ' ')
}
const expr = '2 + 3 * sin(pi / 4) - 4x'
const scope = new Map([
['x', 2]
])
const compiled = math.parse(expr).compile(math)
function undefinedSymbol (name) {
throw new Error('Undefined symbol "' + name + '"')
}
const sin = getSafeProperty(math, 'sin')
const pi = getSafeProperty(math, 'pi')
const compiledPlainJs = {
evaluate: function (scope) {
return 2 + 3 * (scope.has('sin') ? scope.get('sin') : sin)((scope.has('pi') ? scope.get('pi') : pi) / 4) - 4 * (scope.has('x') ? scope.get('x') : undefinedSymbol('x'))
}
}
const correctResult = -3.878679656440358
console.log('expression:', expr)
console.log('scope:', scope)
console.log('result:', correctResult)
assertApproxEqual(compiled.evaluate(scope), correctResult, 1e-7)
assertApproxEqual(compiledPlainJs.evaluate(scope), correctResult, 1e-7)
let total = 0
const nodes = []
const suite = new Benchmark.Suite()
suite
.add(pad('(plain js) evaluate'), function () {
total += compiledPlainJs.evaluate(scope)
})
.add(pad('(mathjs) evaluate'), function () {
total += compiled.evaluate(scope)
})
.add(pad('(mathjs) parse, compile, evaluate'), function () {
total += math.parse(expr).compile().evaluate(scope)
})
.add(pad('(mathjs) parse, compile'), function () {
const node = math.parse(expr).compile()
nodes.push(node)
})
.add(pad('(mathjs) parse'), function () {
const node = math.parse(expr)
nodes.push(node)
})
.on('cycle', function (event) {
console.log(String(event.target))
})
.on('complete', function () {
// we count at total to prevent the browsers from not executing
// the benchmarks ("dead code") when the results would not be used.
if (total > 1e6) {
console.log('')
} else {
console.log('')
}
})
.run()
function assertApproxEqual (actual, expected, tolerance) {
const diff = Math.abs(expected - actual)
if (diff > tolerance) assert.strictEqual(actual, expected)
else assert.ok(diff <= tolerance, actual + ' === ' + expected)
}