Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array find vs sum
(version: 0)
Comparing performance of:
find vs some
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = [ { "code": "FACEBOOK", "description": "Find us on Facebook", "value": "https://www.facebook.com/CDBPLC" }, { "code": "LINKEDIN", "description": "Find us on Linkedin", "value": "https://www.linkedin.com/company/citizens-development-business-f" }, { "code": "CALL_CENTER_TEL", "description": "Call center", "value": "0112380380" }, { "code": "FAX", "description": "Fax", "value": "011222555" }, { "code": "INSTAGRAM", "description": "Find us on Instagram", "value": "https://www.instagram.com/cdb.srilanka/" }, { "code": "ADDRESS", "description": "Address", "value": "123, Orabipasha Mawatha, Colombo 10" }, { "code": "TEL_NO", "description": "Our Hotlines", "value": "+94 117 388 000" }, { "code": "EMAIL", "description": "Email us", "value": "customercare@cdb.lk" }, { "code": "COMP_NAME", "description": "Company Name", "value": "Citizens Development Business Finance PLC" }, { "code": "WEB", "description": "Find us", "value": "https://www.cdb.lk/" } ]; var orderOfDetailArr = [ { code: 'COMP_NAME',icon:'icon-company' }, { code: 'ADDRESS',icon:'icon-locator' }, { code: 'TEL_NO',icon:'icon-phone-ring' }, { code: 'CALL_CENTER_TEL',icon:'icon-phone-ring' }, { code: 'FAX',icon:'icon-fax' }, { code: 'EMAIL',icon:'icon-envlop' }, { code: 'WEB',icon:'icon-website' }, ] var contactCode = orderOfDetailArr.map(({code})=>code);
Tests:
find
array.filter((o1) => orderOfDetailArr.some((o2) => o1.code === o2.code? o1.contact=o2.icon:'')) .sort( (a, b) => contactCode.indexOf(a.code) - contactCode.indexOf(b.code) );
some
array.filter((o1) => { const found = orderOfDetailArr.find(x => x.code === o1.code); if (found) { o1.contact = found.icon; return true; } return false; })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
find
some
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 do my best to explain what's being tested in this benchmark. **Benchmark Overview** This benchmark compares the performance of two JavaScript methods: `filter()` with the `some()` method, and `filter()` without using `some()`. The tests are designed to find a specific code in an array and return the corresponding contact information. **Test Case 1: "find"** In this test case, we have an array of objects (`array`) and another array of codes (`orderOfDetailArr`). We want to find an object in `array` whose `code` matches one in `orderOfDetailArr`. The code uses `filter()` with a callback function that returns `true` if the condition is met. **What's being tested:** * The performance of using `filter()` with a single iteration over `orderOfDetailArr`. * The use of the `some()` method to check if any element in `orderOfDetailArr` matches the `code` property of an object in `array`. **Pros and Cons:** * Using `filter()` with a single iteration is more efficient because it only iterates over `orderOfDetailArr` once. * However, this approach assumes that the matching code will always be found in the array. If not, the method returns an empty array. * The use of `some()` adds some overhead due to function call and parameter passing. **Test Case 2: "some"** In this test case, we have the same setup as before, but instead of using `filter()`, we use a more traditional approach with a for loop that iterates over each element in `orderOfDetailArr`. The code uses `find()` to locate an object in `array` whose `code` matches one in `orderOfDetailArr`. **What's being tested:** * The performance of using a traditional loop-based approach with `find()`. * The use of the `some()` method is not applicable in this case. **Pros and Cons:** * This approach allows for more control over the iteration process, which can be beneficial in certain situations. * However, it's generally less efficient than using `filter()` due to the overhead of function calls and parameter passing. * The use of `find()` adds some overhead compared to a simple lookup. **Library and Special JS Features** In this benchmark, we're using: * The `filter()` method, which is a built-in JavaScript method for filtering arrays. * The `some()` method, which is also a built-in JavaScript method for testing if any element in an array satisfies a condition. * The `find()` method, which is used in the traditional loop-based approach. No special JS features or syntax are being tested in this benchmark. **Alternatives** If you were to rewrite this benchmark using different approaches, some alternatives could be: * Using `map()` instead of `filter()` or `some()`. * Using a more advanced data structure like a trie or a hash table for faster lookup. * Implementing a custom algorithm that uses a different approach to find the matching code. These alternatives would require significant changes to the benchmark setup and code, but could potentially lead to improved performance or efficiency.
Related benchmarks:
find vs includes
Number array indexOf vs includes vs some vs find vs for
find vs some on 10,000 rows
Array.prototype.find vs Lodash find object
#2 Array Includes vs. Find
Comments
Confirm delete:
Do you really want to delete benchmark?