Performance: Findindex Vs Array.prototype.map
Solution 1:
Conceptually, these two snippets accomplish the same goal, but they go about it very different ways. To understand how the two solutions are different, let's first look at findIndex
:
The
findIndex
method executes thecallback
function once for every array index0..length-1
(inclusive) in the array until it finds one wherecallback
returns a truthy value.
In other words, it will stop as soon as it finds the item you're looking for. indexOf
has a similar behavior, because it will return the index of the first item found.
On the other hand, looking at map
:
map
calls aprovided
callback function once for each element in an array, in order, and constructs a new array from the results.
In other words, map
doesn't care what item you're search for. Even if the item you're looking for is the first item in the array, map
will still loop through 14999 other items to create a new array of id
's. This means you'll end up doing quite a lot more work to achieve the same results, both in terms of temporal complexity (more time needed to loop through all those items) and spatial complexity (more memory needed to store that temporary array).
Side note: The above is not necessarily true if you use iterators / generators, which can sort of 'look ahead' in a sense to see if more work is needed. But I think this is outside the scope of this question.
However, if you're really concerned about performance, it's always a good idea to run a test for yourself. Here's a quick benchmark to demonstrate the relative performance of the two implementations. On my machine, I get findIndex: 0.023ms
/ map+indexOf: 0.572ms
. Your mileage may vary somewhat:
var suite = newBenchmark.Suite();
const haystack = [];
let needle;
suite
.add('findIndex', () => haystack.findIndex(o => o.id === needle))
.add('map+indexOf', () => haystack.map(o => o.id).indexOf(needle))
.on('start', () => {
for (let i = 0; i < 15000; i++) {
haystack.push({
id: Math.random().toString(36).substring(7)
});
}
console.log('Starting test.');
})
.on('cycle', () => {
needle = haystack[Math.floor(Math.random() * haystack.length)].id;
})
.on('complete', () => {
console.log('Test results (lower is better):')
suite.forEach((bench) => {
console.log(` ${bench.name}: ${(bench.stats.mean * 1000).toFixed(3)}ms`);
});
})
.run({
'async': true
});
<scriptsrc="//cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script><scriptsrc="//cdnjs.cloudflare.com/ajax/libs/platform/1.3.5/platform.min.js"></script><scriptsrc="//cdnjs.cloudflare.com/ajax/libs/benchmark/2.1.4/benchmark.min.js"></script>
Post a Comment for "Performance: Findindex Vs Array.prototype.map"