ClassList versus ClassName
JavaScript performance comparison
Preparation code
<script>
Benchmark.prototype.setup = function() {
var fooEl = document.createElement("div");
fooEl.className = ' aaaa bbbb cccc dddd ';
// with the leading and trailing spaces
var nowhitespaceEl = document.createElement("div");
nowhitespaceEl.className = 'aaaa bbbb cccc dddd';
// without the leading and trailing spaces
var rtrim = /^\s+|\s+$/g;
};
Benchmark.prototype.teardown = function() {
fooEl = null;
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test | Ops/sec | |
---|---|---|
classList.remove
|
|
pending… |
className.replace
|
|
pending… |
classList.add
|
|
pending… |
className +=
|
|
pending… |
className.replace (many) + cleanup
|
|
pending… |
classList.remove (many)
|
|
pending… |
className.replace with trim
|
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit
to the URL.
- Revision 1: published Steve King
- Revision 2: published Kashey
- Revision 3: published Marcin
- Revision 4: published
- Revision 5: published
- Revision 6: published
- Revision 7: published
- Revision 8: published
- Revision 9: published
- Revision 10: published
- Revision 11: published
- Revision 12: published
- Revision 13: published
- Revision 14: published
- Revision 15: published
- Revision 16: published
- Revision 17: published
0 Comments