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, 4, 2, 2, 1, 5, 3, 5, 1, 4, 3, 4, 2, 2, 1, 5, 3, 5, 4, 3, 4, 2, 2, 1, 5, 3, 5, 1, 4, 3, 4, 2, 2, 1, 5, 3, 5, 1, 4, 3, 4, 2, 2, 1, 5, 3, 5, 1, 4, 3, 4, 2, 2, 1, 5, 3, 5, 1, 4, 3, 4, 2, 2, 1, 5, 3, 5, 4, 3, 4, 2, 2, 1, 5, 3, 5, 1, 4, 3, 4, 2, 2, 1, 5, 3, 5, 1, 4, 3, 4, 2, 2, 1, 5, 3, 5, 1, 4, 3, 4, 2, 2, 1, 5, 3, 5, 1, 4, 3, 4, 2, 2, 1, 5, 3, 5];
const x = _.filter(array, i => i === 3).length
const x = _.sumBy(array, i => i === 3 ? 1 : 0)
const x = array.filter(i => i === 3).length
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Filter length | |
Sum | |
Filter length native |
Test name | Executions per second |
---|---|
Filter length | 67643.4 Ops/sec |
Sum | 87959.8 Ops/sec |
Filter length native | 69018.5 Ops/sec |
Benchmark Overview
MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark definition json and individual test cases are used to compare the execution performance of different approaches for filtering an array in JavaScript.
Benchmark Definition
The benchmark definition provides a set of rules for generating the input data and the executable code to be timed. In this case, there are three benchmark definitions:
const x = _.filter(array, i => i === 3).length
: This definition uses Lodash's filter
function to filter the array and then gets the length of the resulting array.const x = _.sumBy(array, i => i === 3 ? 1 : 0)
: This definition uses Lodash's sumBy
function to sum up the elements in the array that match a certain condition (in this case, elements equal to 3).const x = array.filter(i => i === 3).length
: This definition uses the native JavaScript filter
function to filter the array and then gets the length of the resulting array.Individual Test Cases
Each test case represents an individual iteration of the benchmark. The test cases are:
Filter length
Sum
Filter length native
These test cases are executed multiple times (not specified in the provided data) to generate a distribution of execution times.
Latest Benchmark Result
The latest benchmark result provides a snapshot of the execution performance of each test case on a specific machine:
Filter length
:Filter length native
:Sum
:Comparison
The benchmark results suggest that:
Filter length
) is the fastest, with an execution frequency of approximately 87959 times per second.filter
function implementation (Filter length native
) is slower than the Lodash-based implementation, but still relatively fast, with an execution frequency of approximately 69018 times per second.Sum
test case is the slowest, with an execution frequency of approximately 67643 times per second.Conclusion
The benchmark results suggest that using a library like Lodash can provide a performance boost for certain operations, such as filtering and summing arrays. However, native JavaScript implementations can still be competitive in terms of performance.