Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Search object property value in array of object - 3
(version: 0)
Comparing performance of:
map indexof vs custom func vs findIndex
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var size = 10000 var arr = [] for (let i = 0; i < size; i++) { arr.push({ i }) } var research = 6578; function findObjectInArray(arr, prop, value) { if (arr.length === 0 || !arr[0].hasOwnProperty(prop)) return -1; for (let i = 0; i < size; i++) { if (!arr[i].hasOwnProperty(prop)) continue; if (arr[i][prop] === value) return i; } return -1; }
Tests:
map indexof
const index = arr.map(e => e.i).indexOf(research)
custom func
const index = findObjectInArray(arr, 'i', research)
findIndex
const index = arr.findIndex(e => e.i === research)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
map indexof
custom func
findIndex
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark definition and test cases, explaining what's being tested, compared, and their pros and cons. **Benchmark Definition:** The website `MeasureThat.net` is used to create and run JavaScript microbenchmarks. The provided benchmark definition represents a JavaScript function that searches for a specific property value in an array of objects. **Script Preparation Code:** ```javascript var size = 10000; var arr = []; for (let i = 0; i < size; i++) { arr.push({ i }); } var research = 6578; function findObjectInArray(arr, prop, value) { if (arr.length === 0 || !arr[0].hasOwnProperty(prop)) return -1; for (let i = 0; i < size; i++) { if (!arr[i].hasOwnProperty(prop)) continue; if (arr[i][prop] === value) return i; } return -1; } ``` This script creates an array of objects with a property `i`, sets a research value, and defines the `findObjectInArray` function. This function takes three parameters: the array to search in, the property to search for, and the value to search for. **Html Preparation Code:** (not provided) The HTML preparation code is not shown, but it's likely that it sets up the test environment or initializes any necessary variables. **Individual Test Cases:** There are three individual test cases: 1. **"map indexof"** ```javascript const index = arr.map(e => e.i).indexOf(research); ``` This test case uses the `Array.prototype.map()` method to create a new array with the values of the `i` property, and then uses the `indexOf()` method to search for the research value. 2. **"custom func"** ```javascript const index = findObjectInArray(arr, 'i', research); ``` This test case calls the custom function `findObjectInArray()` with the array `arr`, the property `'i'`, and the research value. 3. **"findIndex"** ```javascript const index = arr.findIndex(e => e.i === research); ``` This test case uses the `Array.prototype.findIndex()` method to search for an element in the array that matches the research value. **Comparison:** The three test cases are compared to determine which one is the fastest. The results show the number of executions per second for each browser. **Pros and Cons:** 1. **"map indexof"** * Pros: + Uses a well-known method for creating an array of values. + Can be faster due to the optimized implementation of `indexOf()` on arrays. * Cons: + Creates a new array with more memory usage. + May require more CPU cycles due to the mapping operation. 2. **"custom func"** * Pros: + Allows for customization of the search logic. + Can be faster if optimized correctly. * Cons: + Requires manual implementation, which can lead to errors or performance issues. 3. **"findIndex"** * Pros: + Optimized for searching a single element in an array. + Can be faster due to the specialized implementation of `findIndex()`. **Library and Purpose:** The `Array.prototype.map()`, `Array.prototype.indexOf()`, and `Array.prototype.findIndex()` methods are built-in JavaScript libraries that provide efficient ways to manipulate arrays. They are widely supported across different browsers and environments. **Special JS Feature or Syntax:** None mentioned. **Alternatives:** Other alternatives for searching an array of objects could include: * Using a library like Lodash, which provides a `findIndex()` method. * Implementing a custom loop-based search function. * Using a data structure like a Set or a Map to store the elements being searched. Keep in mind that the best approach depends on the specific requirements and constraints of the project.
Related benchmarks:
Object property vs array on small array
find in array of objects by field 1002
Search object property value in array of object - 2
Array.prototype.find vs Lodash find object
Comments
Confirm delete:
Do you really want to delete benchmark?