<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var array = new Array(10000).fill().map((v, i) => i);
var result = _.chain(array).map((v) => v + 1)
.filter((v) => v % 3 === 0)
.slice(0, 10).value();
var result = array.map((v) => v + 1)
.filter((v) => v % 3 === 0)
.slice(0, 10);
--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 | 620088.4 Ops/sec |
es6 filter then includes method | 3369.2 Ops/sec |
Let's break down the provided JSON data and explain what's being tested.
Overview
The benchmark compares two approaches to filtering and including elements in an array using the filter
method. The test cases are designed to measure performance differences between using Lodash (a popular JavaScript utility library) versus native JavaScript syntax.
Test Cases
There are two individual test cases:
_.chain()
function from Lodash to chain together three operations:map((v) => v + 1)
increments each element in the array by 1.filter((v) => v % 3 === 0)
filters out elements that are not multiples of 3.slice(0, 10).value()
returns the first 10 elements that passed the filter.array.map((v) => v + 1)
increments each element in the array by 1.filter((v) => v % 3 === 0)
filters out elements that are not multiples of 3.slice(0, 10)
returns a new array with the first 10 elements.Library: Lodash
Lodash is a popular JavaScript utility library that provides a comprehensive set of functions for various tasks, including:
+ Array manipulation (e.g., map
, filter
, reduce
)
+ String manipulation (e.g., trim
, replace
, split
)
+ Object manipulation (e.g., merge
, pick
, assign
)
+ Function manipulation (e.g., bind
, curry
, debounce
)
In this benchmark, Lodash is used to chain together three operations using the _.chain()
function.
Special JS Feature/ Syntax
There isn't any special JavaScript feature or syntax explicitly mentioned in the provided data. However, it's worth noting that some JavaScript features, like async/await or Promises, might be used implicitly when working with callbacks or promises returned by functions like map
and filter
.
Other Alternatives
If you want to compare performance between Lodash and native JavaScript syntax for filtering and including elements in an array, you could also consider using other alternatives like: + Other utility libraries (e.g., Ramda) + Built-in JavaScript methods from earlier versions of the language + Custom implementations using vanilla JavaScript
Keep in mind that these alternatives might have different performance characteristics depending on the specific use case and environment.
Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Ultimately, the choice between Lodash and native JavaScript syntax depends on your specific needs, performance requirements, and personal preferences.