const attrib = [{foo: { bar: { baz: 'a', biz: 'x'}}}, {foo: { bar: { baz: 'b', biz: 'x'}}}, {foo: { bar: { baz: 'x', biz: 'x'}}}]
attrib.find(a => a.foo.bar.baz === 'x' && a.foo.bar.biz === 'x')
const attrib = [{foo: { bar: { baz: 'a', biz: 'x'}}}, {foo: { bar: { baz: 'b', biz: 'x'}}}, {foo: { bar: { baz: 'x', biz: 'x'}}}]
attrib.find(({foo: {bar: { baz, biz }}}) => baz === 'x' && biz === 'x')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Assign | |
Destructure |
Test name | Executions per second |
---|---|
Assign | 2052550.5 Ops/sec |
Destructure | 2068447.1 Ops/sec |
Overview of the Benchmark
The provided benchmark, hosted on MeasureThat.net, tests the performance of two approaches to find a specific value in an array of objects: assigning values directly versus destructuring objects.
Benchmark Definition JSON
The benchmark definition JSON contains two test cases:
find()
method with a callback function that checks for equality.find()
method with a new callback function.Options Compared
The benchmark compares two approaches:
find()
method with a callback function that assigns values directly.find()
method with a new callback function.Pros and Cons of Each Approach
Library and Special JS Features
In this benchmark, the find()
method is used to search for an element in an array. The find()
method is a standard JavaScript method that is widely supported by most browsers and environments.
There are no special JavaScript features or syntaxes used in this benchmark beyond ES6+ syntax (object destructuring).
Other Alternatives
If you want to explore other alternatives, here are some options:
find()
method, you could use a linear search or another algorithm that is optimized for performance.Benchmark Preparation Code
The provided benchmark preparation code is minimal and consists of two scripts:
find()
method.These scripts are designed to be simple and straightforward, allowing users to easily reproduce and compare the results of their own benchmarks.