mathjs/benchmark/expression_parser.js
greenkeeper[bot] c5971b371a Update standard to the latest version 🚀 (#1226)
* chore(package): update standard to version 12.0.0

* update to new lint version with --fix

I believe this mainly adds whitespace to `{}`'s.

* Replace assert.equal with assert.strictEqual

This breaks a lot of tests which I will endevour to fix in the next
commits.

* Fix most errors due to assert.strictEquals

Some instances of `strictEquals` are replaced by `deepEquals`.
`toString` has been used to make some string comparisions explicit.
Tests will still fail untill #1236 and #1237 are fixed.

* Fix assertion erros due to -0

With node 10, assert.strictEqual no longer considers `0 === -0`.
I missed these first time round as I was using node 8.

* Put toString correct side of bracket

I was converting the constructor to a string rather
than the result of the computation. Oops.

* Fixed #1236: quantileSeq has inconsistant return

* Update package-lock

* Fixed #1237: norm sometimes returning a complex number instead of number

* Fix cli tests

* More changes for standardjs, and fixes in unit tests
2018-09-08 16:33:58 +02:00

84 lines
2.3 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('../index')
const getSafeProperty = require('../lib/utils/customs').getSafeProperty
// 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'
let scope = { x: 2 }
const compiled = math.parse(expr).compile(math, {})
const sin = getSafeProperty(math, 'sin')
const pi = getSafeProperty(math, 'pi')
const compiledPlainJs = {
eval: function (scope) {
return 2 + 3 * ('sin' in scope ? getSafeProperty(scope, 'sin') : sin)(('pi' in scope ? getSafeProperty(scope, 'pi') : pi) / 4) - 4 * scope['x']
}
}
const correctResult = -3.878679656440358
console.log('expression:', expr)
console.log('scope:', scope)
console.log('result:', correctResult)
assertApproxEqual(compiled.eval(scope), correctResult, 1e-7)
assertApproxEqual(compiledPlainJs.eval(scope), correctResult, 1e-7)
let total = 0
let nodes = []
const suite = new Benchmark.Suite()
suite
.add(pad('(plain js) evaluate'), function () {
total += compiledPlainJs.eval(scope)
})
.add(pad('(mathjs) evaluate'), function () {
total += compiled.eval(scope)
})
.add(pad('(mathjs) parse, compile, evaluate'), function () {
total += math.parse(expr).compile().eval(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)
}