Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test samples
(version: 0)
Test
Comparing performance of:
Old method vs New check vs New replace
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var domEl = document.createElement("span"), cls = " aaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbb ", _hasCls = function(domEl, cls) { var found = false, pos = domEl.className.indexOf(cls); if (pos >= 0) { var endpos = pos + cls.length; found = endpos == domEl.className.length || domEl.className.charAt(endpos) == ' '; } return found; };
Tests:
Old method
if (!_hasCls(domEl, cls)) { if (domEl.className.length > 0) { cls = ' ' + cls; } domEl.className += cls; }
New check
cls.split(' ').forEach(function(onecls) { if (onecls) domEl.classList.add(onecls); });
New replace
cls.replace(/\s+/g, ' ').trim().split(' ').forEach(function(onecls) { domEl.classList.add(onecls); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Old method
New check
New replace
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!
Related benchmarks:
Test samples
$.hasClass vs RegExp vs indexOf vs classList
$.hasClass vs RegExp vs indexOf vs classList 2
createElement("span") vs cloneNode()
Comments
Confirm delete:
Do you really want to delete benchmark?