Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
FIND() vs OBJ - Operações de pesquisa em JS
(version: 0)
Comparing performance of:
find vs object
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function generateUUID() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { var r = (Math.random() * 16) | 0, v = c == 'x' ? r : (r & 0x3) | 0x8; return v.toString(16); }); } var objectsObject = {}; var itemsArray = []; const item = { "id": "72fdff2b-1fef-4d5a-944b-bf04e27ccd73", "title": "Teste", "type": "text", "tags": [] } const object = { "72fdff2b-1fef-4d5a-944b-bf04e27ccd73": { "title": "Teste", "type": "text", "tags": [] } } function generateItem() { return { id: generateUUID(), title: 'Teste', type: 'text', tags: [] }; } function generateObject() { let uuid = generateUUID(); return { [uuid]: { title: 'Teste', type: 'text', tags: [] } }; } // Gerar um objeto com chaves como IDs for (let i = 0; i < 300000; i++) { const obj = generateObject(); const key = Object.keys(obj)[0]; // Obter a chave do objeto gerado objectsObject[key] = obj[key]; // Adicionar o objeto ao objeto objectsObject } objectsObject["72fdff2b-1fef-4d5a-944b-bf04e27ccd73"] = object; for (let i = 0; i < 300000; i++) { itemsArray.push(generateItem()); } itemsArray.push(item);
Tests:
find
itemsArray.find((menssage) => menssage.id === "72fdff2b-1fef-4d5a-944b-bf04e27ccd73")
object
objectsObject["72fdff2b-1fef-4d5a-944b-bf04e27ccd73"];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
find
object
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
find
184.5 Ops/sec
object
7853405.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. **Benchmark Overview** The benchmark is comparing two approaches for searching data in JavaScript: 1. Using an array (`itemsArray`) with a `find()` method to search for a specific element. 2. Directly accessing an object by its key (`objectsObject`). **Options Compared** The two options are compared in terms of execution speed, measured in executions per second. **Pros and Cons of Each Approach** 1. **Array Search (find())** * Pros: + Widely supported by modern browsers. + Easy to implement and understand. + Can be used for more complex searches (e.g., filtering, sorting). * Cons: + May not perform well on very large datasets due to the overhead of searching through the array. + May require additional processing steps to handle edge cases (e.g., empty arrays, null values). 2. **Direct Object Access** * Pros: + Can be faster for direct accesses, especially when working with small to medium-sized datasets. + Reduces memory allocation and garbage collection overhead. * Cons: + Requires the object key to be known in advance. + May not work well with large or dynamic datasets. **Library and Syntax** The benchmark uses the `generateUUID()` function, which is a simple implementation of a UUID (Universally Unique Identifier) generator. This function is used to create unique IDs for objects and items. No special JavaScript features or syntax are being tested in this benchmark. However, it's worth noting that using `Object.keys()` to get an array of object keys can be a costly operation due to the need to traverse the object's prototype chain. **Alternatives** If you need to search through large datasets, other approaches might include: 1. **Using a data structure like a hash table or a map**, which allows for fast lookups and updates. 2. **Implementing a custom search algorithm**, such as binary search or interval searching. 3. **Using a library like Lodash** or **Ramda**, which provide optimized functions for common tasks like array searching. Keep in mind that the choice of approach depends on the specific requirements of your use case, including factors like dataset size, performance constraints, and code maintainability.
Related benchmarks:
Delete vs destructure for cloned objects
Delete vs destructure for objects 2
Delete vs destructure for objects - guigo2
Delete vs destructure for objects without mutating pedro
Object reduction with conversion back (smol)
Comments
Confirm delete:
Do you really want to delete benchmark?