<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var array = [1,2,3,4,5,6,7,8,9];
var result = _.chain(array).filter(n => n > 5).includes(5);
var result = array.filter(n => n > 5).includes(5);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash filter then includes method | |
es6 filter then includes method |
Test name | Executions per second |
---|---|
lodash filter then includes method | 3962020.8 Ops/sec |
es6 filter then includes method | 18073710.0 Ops/sec |
Let's break down the provided benchmark definition and test cases to understand what is being tested.
Benchmark Definition:
The benchmark compares the performance of two approaches:
Array.prototype.filter()
and Array.prototype.includes()
are being compared.Script Preparation Code:
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9];
This creates a sample array of numbers for the benchmark.
Html Preparation Code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
This includes the Lodash library in the HTML file.
Test Cases:
There are two individual test cases:
Benchmark definition:
var result = _.chain(array).filter(n => n > 5).includes(5);
This uses the _.chain()
method from Lodash to create a new chain of operations, followed by filter()
, and finally includes()
.
Benchmark definition:
var result = array.filter(n => n > 5).includes(5);
This uses the native JavaScript methods Array.prototype.filter()
and Array.prototype.includes()
directly on the sample array.
Pros and Cons of Each Approach:
Library:
Lodash is a popular JavaScript utility library that provides a lot of useful functions for tasks like array manipulation, string manipulation, and more. It can be used to simplify code and improve readability.
Special JS Feature/Syntax:
There are no special JavaScript features or syntax being tested in this benchmark. The focus is on comparing the performance of using Lodash versus native JavaScript methods.
Other Considerations:
Alternatives:
If you want to compare other JavaScript libraries or utility functions, you can modify the benchmark definition and script preparation code. Some alternatives could be:
Array.prototype.map()
, Array.prototype.reduce()
, or String.prototype.split()
.