<script src="lodash.js"></script>
var array = [Array(100000).keys()];
array.splice(55555,1)
_.filter(array,n => n !== 55555)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Array.prototype.splice | |
Lodash filter |
Test name | Executions per second |
---|---|
Array.prototype.splice | 17883082.0 Ops/sec |
Lodash filter | 1979.2 Ops/sec |
Let's break down the provided benchmark and explain what's being tested, compared, and considered.
Benchmark Definition and Script Preparation Code
The benchmark measures the performance of two approaches: Array.prototype.splice
and _.filter
from Lodash library.
The script preparation code creates an array with 100,000 elements using the spread operator (var array = [...Array(100000).keys()]
). This creates a large dataset for testing.
Html Preparation Code
The HTML preparation code includes a script tag that loads the Lodash library. This is necessary to use the _.filter
function in the benchmark.
Options Compared
Two options are compared:
Pros and Cons of Each Approach
Array.prototype.splice
Pros:
Cons:
_.filter
Pros:
Cons:
Library: Lodash
Lodash is a utility library that provides various functions for tasks like array manipulation, string manipulation, and more. In this case, _.filter
is used to create a new array containing only the elements that satisfy the given condition (in this case, not equal to 55555).
Special JS Feature or Syntax
None mentioned in this benchmark.
Other Considerations
Alternatives
Other alternatives for filtering arrays could include:
Array.prototype.map
instead of _.filter
, which would create a new array without modifying the original one.Keep in mind that each alternative has its pros and cons, and the best approach depends on the specific use case and performance requirements.