-
|
I'm using mathjs to generate the mathematical functions dynamically for evaluation, In the benchmark: Even with |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
|
Two things here:
|
Beta Was this translation helpful? Give feedback.
-
|
Thank you very much for building such a powerful math library. What might be the reason for this performance difference? And do you have any suggestions about my work? var script = document.createElement('script');
script.src = 'https://cdnjs.cloudflare.com/ajax/libs/mathjs/6.6.1/math.min.js';
document.head.appendChild(script);
var average = 0;
for(var test = 0; test<10; test++){
var startTime = performance.now();
for (var i = 0; i < 50000; i++) {
let x = math.evaluate('sqrt(3^2 + 4^2)');
}
var endTime = performance.now();
var elapsedTime = endTime - startTime;
average += elapsedTime;
}
console.log("mathjs evaluate average result"+ average/10 + " ms");
var average = 0;
for(var test = 0; test<10; test++){
var startTime = performance.now();
for (var i = 0; i < 50000; i++) {
let x = eval('Math.sqrt(3**2 + 4**2)');
}
var endTime = performance.now();
var elapsedTime = endTime - startTime;
average += elapsedTime;
}
console.log("eval average result"+ average/10 + " ms");result is: |
Beta Was this translation helpful? Give feedback.

Two things here:
fn(x, y, z)in your benchmarkmath.addandmath.powthat are capable of handling numbers, BigNumbers, Units, Fractions, Matrices, etc. If you only neednumbers, you can speed things up a lot by creating a mathjs instance with lightweight functions. To do that, you can use the plain-number implementation viaimport {...} from 'mathjs/number'(see docs), or by providing alternative function implementations yourself. Examples: