<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]
_.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 | 17537600.0 Ops/sec |
array filter | 52649124.0 Ops/sec |
Let's break down the provided JSON and explain what's being tested.
Benchmark Definition:
The benchmark is comparing two approaches to filter an array of numbers: using Lodash's _filter
function and using JavaScript's built-in filter
method.
Script Preparation Code:
This code snippet defines a variable numbers
which is an array of integers:
var numbers = [10, 40, 230, 15, 18, 51, 1221];
Html Preparation Code:
This line includes the Lodash library from a CDN:
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>
Individual Test Cases:
There are two test cases:
_.filter(numbers, num => num % 3 === 0)
: This line calls the _filter
function from Lodash on the numbers
array, filtering out numbers that are not divisible by 3.numbers.filter(num => num % 3 === 0)
: This line uses JavaScript's built-in filter
method to achieve the same result as the previous test case.Pros and Cons of Each Approach:
_filter
function:filter
method:_filter
Other Considerations:
_filter
function may provide additional benefits, such as caching and memoization, which are not present in the JavaScript filter
method.Special JS Features/Syntax:
None mentioned. However, it's worth noting that the benchmark is using ES6-style arrow functions (num => num % 3 === 0
) for simplicity and readability.
Library Used:
Lodash (version 4.17.11) provides the _filter
function used in the first test case.
Device/Platform Considerations:
The benchmark result includes data from a Chrome 131 browser running on a Mac OS X 10.15.7 device, which is an example of the specific device/platform being tested.