<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var value = {a: 30310, b: 100303, c: 3040494, d: null, e: undefined, f: 'aaaa', g: [], i: {i1: 'test', i2: 7}, k: 0, l: ''};
_.some(value, (v) => v)
Object.values(value).some((v) => v)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
_.some() | |
Native |
Test name | Executions per second |
---|---|
_.some() | 4017723.2 Ops/sec |
Native | 4592780.5 Ops/sec |
I'd be happy to explain the benchmark and provide insights into the test cases.
Benchmark Overview
The benchmark, created on MeasureThat.net, compares the performance of two approaches: using JavaScript's built-in Object.values()
and some()
method versus using the popular utility library Lodash's _some()
function. The goal is to determine which approach yields better performance in terms of speed and efficiency.
Test Cases
There are two test cases:
Object.values()
returns an array of the object's own enumerable property values, and some()
checks if any of these values pass a provided test function._some()
function, which is a wrapper around the native JavaScript implementation.Options Compared
The two options being compared are:
Object.values().some()
)_some()
functionPros and Cons of Each Approach
Native JavaScript Implementation (Object.values().some()):
Pros:
Cons:
some()
.Lodash's _some()
Function:
Pros:
Cons:
Library (Lodash)
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks such as array manipulation, string manipulation, object manipulation, and more. _some()
is one of these functions, which checks if at least one element in an array passes a test function.
Special JS Feature/Syntax (Not Applicable)
In this benchmark, there are no special JavaScript features or syntax being used that would impact the performance comparison.
Other Alternatives
If you're interested in exploring other alternatives for checking if any elements in an array pass a test function, here are some options:
forEach()
and checking for truthiness: Array.prototype.forEach.call(array, (element) => { return element; });
every()
and negating the result: !Array.prototype.every.call(array, (element) => { return !element; })
Keep in mind that these alternatives may have different performance characteristics compared to the native implementation or Lodash's _some()
function.
I hope this explanation helps!