1 |
|
2 |
|
3 | var Benchmark = require('benchmark');
|
4 | var padRight = require('pad-right');
|
5 | var math = require('../index');
|
6 |
|
7 | function pad (text) {
|
8 | return padRight(text, 40, ' ');
|
9 | }
|
10 |
|
11 | var expr = '2 + 3 * sin(pi / 4) - 4x';
|
12 | var scope = {x: 2};
|
13 | var compiled = math.parse(expr).compile();
|
14 |
|
15 | console.log('expression:', expr);
|
16 |
|
17 | var suite = new Benchmark.Suite();
|
18 | suite
|
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();
|