<script src="https://cdn.jsdelivr.net/npm/lodash@4.14.184/lodash.min.js"></script>
const limit = 10000000;
var array = [];
for (let i = 0; i < limit; i++) {
array.push(i);
}
function getRandomIndex() {
return Math.floor(Math.random() * array.length)
}
var element = array[getRandomIndex()];
for (let index = 0; index < this.chips.length; index++) {
if (this.chips[index] == chip) {
this.chips.splice(index,1);
index--;
}
}
_.remove(array, x => x === element);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
native | |
lodash |
Test name | Executions per second |
---|---|
native | 1.5 Ops/sec |
lodash | 1.4 Ops/sec |
Benchmark Explanation
MeasureThat.net is used to compare the performance of two approaches: native JavaScript removal from an array and using the Lodash library.
The benchmark definition JSON represents two test cases:
element
) from an array (array
).remove()
function, to achieve the same result as the native approach.Options Comparison
The two approaches differ in their implementation details:
remove()
function, which takes two arguments: the array to modify and a predicate function that defines the condition for removal.Pros and Cons
Here's a brief analysis of each approach:
Library: Lodash
Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array manipulation, string processing, and more. The remove()
function used in the benchmark definition takes two arguments:
array
: The input array to modify.predicate
: A function that defines the condition for removal.In this case, the predicate function is x => x === element
, which removes the first occurrence of the target element (element
) from the array.
JavaScript Features
Neither the native nor Lodash approaches utilize any special JavaScript features or syntax. They rely on standard JavaScript language constructs and libraries (Lodash).