Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Check attributes
(version: 1)
Comparing performance of:
for loop vs string
Created:
9 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const CLOSE_KEYWORDS = ['close', 'exit', 'quit', 'cancel', 'dismiss']; const attrCount = 1000; // adjust for testing const matchIndex = 0; // index where matching attribute is placed (0 = early match) const el = document.createElement('div'); // Generate many attributes for (let i = 0; i < attrCount; i++) { const value = (i === matchIndex) ? 'please close this' : `attr${i}`; el.setAttribute(`data-${i}`, value); } // Warmup checkWithEarlyExit(el, CLOSE_KEYWORDS); checkWithFullJoin(el, CLOSE_KEYWORDS);
Tests:
for loop
function checkWithEarlyExit(el, keywords) { for (const attr of el.attributes) { const val = attr.value.toLowerCase(); if (keywords.some(keyword => val.includes(keyword))) { return true; } } return false; }
string
function checkWithFullJoin(el, keywords) { const allAttrValues = Array.from(el.attributes, attr => attr.value.toLowerCase()).join(' '); return keywords.some(keyword => allAttrValues.includes(keyword)); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for loop
string
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for loop
285876448.0 Ops/sec
string
255186720.0 Ops/sec
Related benchmarks:
fastest start width
using getAttribute vs property to access an element's id
Compare detecting object keys starting with "on"
Compare createElement vs template String
Compare createElement vs template String - 5 times
Compare createElement vs template String - 100 times
querySelectorAll.length vs getAttribute
using getAttribute vs innerHtml
xxxx15
Comments
Confirm delete:
Do you really want to delete benchmark?