jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Simple test to measure the performance difference when a multitude of possible results need to be computed in such a way that using a dictionary to select them would be the best design, but computing the results before storing them in the dictionary would lead to a lot of unnecessary computation.
<script>
function longCalculation(result) {
window.setTimeout(function() { return result; }, 100);
}
var choices = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"];
var choice;
function simpleConditionalCalculation(key) {
var simpleDict = {};
for (i=0; i<choices.length; i++) {
simpleDict[choices[i]] = longCalculation(null);
};
return simpleDict[key];
}
function lazyConditionalCalculation(key) {
var lazyDict = {};
for (i=0; i<choices.length; i++) {
lazyDict[choices[i]] = function() { return longCalculation(null); };
};
return lazyDict[key]();
}
</script>
choice = choices[Math.floor(Math.random() * choices.length)];
Ready to run.
Test | Ops/sec | |
---|---|---|
Simple conditional calculation |
| ready |
Lazy conditional calculation |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.