UNPKG

970 BJavaScriptView Raw
1// test performance of the expression parser in node.js
2
3var Benchmark = require('benchmark');
4var padRight = require('pad-right');
5var math = require('../index');
6
7function pad (text) {
8 return padRight(text, 40, ' ');
9}
10
11var expr = '2 + 3 * sin(pi / 4) - 4x';
12var scope = {x: 2};
13var compiled = math.parse(expr).compile();
14
15console.log('expression:', expr);
16
17var suite = new Benchmark.Suite();
18suite
19 .add(pad('expression parse and evaluate'), function() {
20 var res = math.eval(expr, scope);
21 })
22 .add(pad('expression parse and compile'), function() {
23 var c = math.parse('2 + 3 * sin(pi / 4) - 4x').compile();
24 })
25 .add(pad('expression parse'), function() {
26 var node = math.parse('2 + 3 * sin(pi / 4) - 4x');
27 })
28 .add(pad('evaluate'), function() {
29 var res = compiled.eval(scope);
30 })
31 .on('cycle', function(event) {
32 console.log(String(event.target));
33 })
34 .on('complete', function() {
35 })
36 .run();