<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.core.js"></script>
var users = [
{ 'user': 'joey', 'age': 32 },
{ 'user': 'ross', 'age': 41 },
{ 'user': 'chandler', 'age': 39 }
]
// Native
users.find(function (o) { return o.age < 40; })
users.find(o => o.age < 40)
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
array normal fucntion find | |
array arrow fucntion find |
Test name | Executions per second |
---|---|
array normal fucntion find | 18050210.0 Ops/sec |
array arrow fucntion find | 18074546.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares two approaches for finding an element in an array: traditional function-based approach using concat()
method, and arrow function-based approach using the new ES6 spread operator (=>
syntax).
Options Compared
Two options are compared:
=>
) to achieve the same result.Pros and Cons
Here's a brief overview of the pros and cons of each approach:
Traditional Function-Based Approach
Pros:
Cons:
concat()
method overheadArrow Function-Based Approach with Spread Operator
Pros:
Cons:
Library Used: Lodash.js
The benchmark uses the Lodash.js library, which provides a find()
method for array filtering. The library is not essential to the comparison itself but is used to provide a standardized way to execute the benchmarks.
Special JS Feature/Syntax: ES6 Spread Operator
The benchmark takes advantage of the new ES6 spread operator (=>
) syntax, which allows for concise and expressive arrow function definitions. This feature is not necessary for understanding the basic concepts of array filtering, but it's an important aspect of modern JavaScript development.
Other Alternatives
If you're interested in exploring other approaches to finding elements in arrays, here are some alternatives:
Array.prototype.find()
method: This is a more modern approach that's gaining popularity. It's similar to the arrow function-based approach but uses a built-in method instead.Overall, this benchmark provides a useful comparison between two approaches for finding elements in arrays, highlighting the benefits and trade-offs of each method.