<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>
var numbers = [10, 40, 230, 15, 18, 51, 1221, 24, 36, 28, 10, 6, 8, 7, 55, 89, 256, 102, 1, 23, 41, 38, 7, 2, 5, 9, 60]
var input = []
Array(1000).fill().forEach(_ => input.push(numbers))
input.filter(i => i > 10).map(x => x * x)
let result = []
for (let x in input) {
if (x > 10) result.push(x * x)
}
let result = []
input.forEach(x => {
if (x > 10) result.push(x * x)
})
_.chain(input).filter(x => x > 10).map(x => x * x).value()
_.map(_.filter(input, x => x > 10), x => x * x)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
native built-in filter | |
native logic for filter, map | |
using forEach | |
lodash chain | |
lodash not chain |
Test name | Executions per second |
---|---|
native built-in filter | 699.1 Ops/sec |
native logic for filter, map | 50.9 Ops/sec |
using forEach | 1825.8 Ops/sec |
lodash chain | 439.0 Ops/sec |
lodash not chain | 452.6 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested on MeasureThat.net.
Benchmark Overview
The benchmark measures the performance of different approaches to filtering and mapping large arrays in JavaScript. The input array contains 20,000 elements, which are then filtered and mapped using various methods.
Options Compared
There are four options compared:
filter()
method directly on the array.for
loop to filter and map the elements.forEach()
method to iterate over the array and apply the filtering and mapping logic.chain()
function to create a pipeline of operations that can be applied sequentially.Pros and Cons
Here's a brief overview of each approach:
Library and Purpose
The benchmark includes Lodash as a reference implementation for the chain()
function. Lodash is a popular JavaScript library that provides various utility functions for tasks like array manipulation, string manipulation, and more.
Special JS Feature or Syntax
None of the approaches rely on any special JavaScript features or syntax beyond what's considered standard in modern JavaScript development.
Other Alternatives
If you're interested in exploring other options, here are a few alternatives:
and
Array.prototype.some()`: These methods can be used to filter arrays using conditional logic.I hope this explanation helps you understand the benchmark and its options better!