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
Check how a (IMO confusing) two level nested _.map (or any fixed level for that matter - it just makes it even more confusing) performs compared to an arbitrarily level _.map that uses recursion.
<script src="https://cdn.jsdelivr.net/lodash/2.4.1/lodash.min.js"></script>
// Recursive _.map implementation for deep mapping
_.mixin({
'deepMap': function(collection, callback, thisArg) {
callback = _.createCallback(callback, thisArg, 3);
return _.map(collection, function(value, index) {
// if it's a number, map it to the requested object {x: index, y: value}
if (_.isNumber(value)) {
return callback(value, index);
}
// if it's an array, map recursively inside it
if (_.isArray(value))
return _.deepMap(value, callback);
return value;
});
}
});
// Fixed implementation for two dimensional mapping
_.mixin({
'twoLvlMap': function(collection, callback, thisArg) {
callback = _.createCallback(callback, thisArg, 3);
// loop through all elements of collection and map them to...
return _.map(collection, function(item) {
// ...the result from running the mapping function on all elements at second level
return _.map(item, callback);
});
}
});
// Fixed implementation for three dimensional mapping
_.mixin({
'threeLvlMap': function(collection, callback, thisArg) {
callback = _.createCallback(callback, thisArg, 3);
// loop through all elements of collection and map them to...
return _.map(collection, function(item) {
// the result of looping through all elements of collection and mapping them to...
return _.map(item, function(item) {
// ...the result from running the mapping function on all elements on third level
return _.map(item, callback);
});
});
}
});
var testArray2Lvl = [
[1, 2, 3],
[11, 12, 13],
[21, 22, 23]
];
var testArray3Lvl = [
[
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
],
[
[11, 12, 13],
[14, 15, 16],
[17, 18, 19]
],
[
[21, 22, 23],
[24, 25, 26],
[27, 28, 29]
]
];
// Elements on the lowest (leaf) level should be mapped t an object
var testCallback = function(value, index) {
return {
x: parseInt(index),
y: value || 0
};
};
Ready to run.
Test | Ops/sec | |
---|---|---|
Two Level Nested |
| ready |
Two Level Recursive |
| ready |
Three Level Nested |
| ready |
Three Level Recursive |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.