<script src='https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js'></script>
var numbers = [10, 40, 230, 15, 18, 51, 1221]
_.filter(numbers, num => num % 3 === 0)
numbers.filter(num => num % 3 === 0)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.filter | |
array filter |
Test name | Executions per second |
---|---|
_.filter | 3576170.2 Ops/sec |
array filter | 7598311.5 Ops/sec |
Let's break down the benchmark and analyze what's being tested.
What is being tested?
The provided JSON represents two test cases: _.filter
(using the Underscore.js library) and array filter
(native JavaScript method). Both tests are designed to measure the performance of filtering an array of numbers where each number has a remainder of 0 when divided by 3.
Options compared
In this benchmark, we have two approaches:
_.filter
function from the Underscore.js library, which is a popular utility library for JavaScript.filter()
method on an array.Pros and Cons of each approach
Underscore.js Library
In this benchmark, the _.filter
function is used from the Underscore.js library. The Underscore.js library provides a set of utility functions that can simplify common tasks in JavaScript. In this case, it's being used to filter an array based on a condition.
Native JavaScript Syntax (ES6+)
The benchmark doesn't explicitly use any special ES6+ features or syntax beyond the const
keyword and template literals for string manipulation. However, if we were to rewrite the native JavaScript approach using more modern syntax, it might look something like this:
numbers.filter(num => num % 3 === 0);
This code uses an arrow function as the callback for the filter()
method.
Other alternatives
If we wanted to explore other approaches, here are a few options:
filter()
.reduce()
and map()
methods in combination.However, these alternatives would likely result in different performance characteristics and might not be as straightforward to implement as the _.filter and array filter approaches.