Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Testing string for one of multiple substring
(version: 0)
Comparing performance of:
Object.keys vs for in
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
window.productNames = { ATOM_HOME_FULL: 'string', ATOM_HOME_SHORT: 'string', ATOM_SCHOOL_FULL: 'string', ATOM_SCHOOL_SHORT: 'string' }
Tests:
Object.keys
Object.keys(window.productNames).some(name => name.includes('SCHOOL'))
for in
for (key in window.productNames) { window.productNames[key].includes('SCHOOL') }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Object.keys
for in
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):
Let's break down the provided benchmark and explain what's being tested, compared, and the pros/cons of different approaches. **Benchmark Overview** The benchmark measures the performance of two JavaScript methods: `Object.keys()` and `for...in` loop with object iteration. The script preparation code initializes an object `window.productNames` with multiple properties containing strings that include a specific substring ("SCHOOL"). **Script Preparation Code** ```javascript window.productNames = { ATOM_HOME_FULL: 'string', ATOM_HOME_SHORT: 'string', ATOM_SCHOOL_FULL: 'string', ATOM_SCHOOL_SHORT: 'string' } ``` This code creates an object with four properties, each containing a string. The strings contain the substring "SCHOOL", which is used to test the benchmark. **Html Preparation Code** There is no HTML preparation code provided, so we'll focus on the JavaScript aspects of the benchmark. **Test Cases** The benchmark consists of two test cases: 1. **Object.keys()** ```javascript Object.keys(window.productNames).some(name => name.includes('SCHOOL')) ``` This code uses the `Object.keys()` method to get an array of property names from the `window.productNames` object and then uses the `some()` method to check if any of those properties include the substring "SCHOOL". 2. **for...in loop** ```javascript for (key in window.productNames) { window.productNames[key].includes('SCHOOL') } ``` This code uses a traditional `for...in` loop to iterate over the properties of the `window.productNames` object and checks if each property value includes the substring "SCHOOL". **Pros and Cons** Here's a brief overview of the pros and cons of each approach: 1. **Object.keys() with some()** * Pros: + More concise and expressive code + Faster iteration due to the use of `some()` * Cons: + May have higher overhead for iterating over properties (due to the use of `some()`) 2. **for...in loop** * Pros: + More traditional and familiar syntax for many developers + Can be more efficient for older browsers that don't support `Object.keys()` * Cons: + Less concise code + May have higher overhead due to the use of the loop and iteration **Library** The benchmark uses no external libraries, relying solely on standard JavaScript features. **Special JS Feature/Syntax** There are no special JS features or syntax used in this benchmark. The focus is on comparing two basic iteration methods. **Other Alternatives** For comparison purposes, other alternatives could be: 1. **Array.prototype.filter()**: `window.productNames.keys().filter(name => name.includes('SCHOOL')).length > 0` 2. **forEach()**: `for (key in window.productNames) { if (window.productNames[key].includes('SCHOOL')) console.log(key); }` However, these alternatives would likely have similar performance characteristics to the original `Object.keys()` and `for...in` loop methods. Overall, this benchmark provides a simple and straightforward way to compare the performance of two iteration methods in JavaScript.
Related benchmarks:
swdfdw
first test undeclared
substr vs substring
Test substring
Comments
Confirm delete:
Do you really want to delete benchmark?