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,4,4,4,4,4,4,4,4,4,4,4,4,4,2,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,2,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,2,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,2,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4]
Array.prototype.flatMap = function(f) {
return [].concat(this.map(f))
}
_.flatMap(x,x => [x-1, x, x+1, x +2, x +2, x +2, x +2,x-1, x, x+1, x +2, x +2, x +2, x +2,x-1, x, x+1, x +2, x +2, x +2, x +2])
x.flatMap(x => [x-1, x, x+1, x +2, x +2, x +2, x +2,x-1, x, x+1, x +2, x +2, x +2, x +2,x-1, x, x+1, x +2, x +2, x +2, x +2])
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash.flatMap | |
native |
Test name | Executions per second |
---|---|
lodash.flatMap | 11120.4 Ops/sec |
native | 9218.0 Ops/sec |
Let's break down the benchmark and explain what's being tested, compared options, pros and cons of those approaches, and other considerations.
Benchmark Definition
The benchmark is testing two ways to implement the flatMap
function in JavaScript:
flatMap
function in JavaScript.Options being compared
We have two options being compared:
flatMap
function from the Lodash library to flatten an array.flatMap
function in JavaScript, which is implemented in V8 (the JavaScript engine used by Chrome).Pros and cons of each approach
Lodash implementation
Pros:
Cons:
Native implementation
Pros:
Cons:
Other considerations
Now that we've broken down the benchmark, let's move on to analyzing the test results.