<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.core.js"></script>
var a = [{marble: "hihi0", text:'hello'}, {marble: "hihi1", text:'a'}, {marble: "hihi2", text:'bc'}];
var b = a.find(item => item.marble === 'hihi1');
var a = [{marble: "hihi0", text:'hello'}, {marble: "hihi1", text:'a'}, {marble: "hihi2", text:'bc'}];
var b = _.find(a, {marble: "hihi1"});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
object array find | |
object array _.find |
Test name | Executions per second |
---|---|
object array find | 18489498.0 Ops/sec |
object array _.find | 4466509.5 Ops/sec |
Let's break down the provided benchmark and explain what is tested, compared options, pros and cons, and other considerations.
Benchmark Overview
MeasureThat.net is a website where users can create and run JavaScript microbenchmarks to compare different approaches. The provided benchmark compares two ways of finding an object in an array: using the find
method without any additional library (built-in JavaScript) versus using the _
variable from the Lodash library.
Options Compared
Two options are compared:
find
method: This is a standard JavaScript method that searches for an element with a given condition in an array._find
function: Lodash is a popular JavaScript utility library that provides various functions, including _find
, which performs the same operation as the built-in find
method.Pros and Cons
find
method:_find
function:find
method due to optimization, and provides additional functionality (e.g., support for promises).Library Used
In this benchmark, Lodash is used. Specifically, it's version 4.17.5. The _
variable from Lodash is used to invoke the _find
function. The purpose of Lodash is to provide a set of reusable functions that make common programming tasks easier and more efficient.
Special JS Features or Syntax
There are no special JavaScript features or syntax mentioned in this benchmark. Both examples use standard JavaScript syntax for defining arrays, variables, and conditional statements.
Other Considerations
_find
function might be faster than the built-in find
method._find
functionality yourself, which can add development overhead and complexity.Alternatives
If you don't want to use an external library like Lodash, you could:
_find
function using a standard JavaScript for
loop or other methods.lodash-es
version (ES6+ compatible) or the underscore
library.Keep in mind that alternatives might not offer the same level of optimization and performance as Lodash's _find
function.