tagName vs instanceof
Date tested:
4 months ago
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:126.0) Gecko/20100101 Firefox/126.0
Test name
Executions per second
instanceof
13722530.0 Ops/sec
tagName
10192670.0 Ops/sec
nodeName
9880119.0 Ops/sec
localName
9334563.0 Ops/sec
__proto__
10116120.0 Ops/sec
Benchmark definition (click to collapse):
Script Preparation code:
var xs = []; var n = 0; for (let i = 0; i < 1000; ++i) { xs[i] = (Math.random() > .5) ? document.createElement(`div`) : document.createElement(`span`); };
Tests:
instanceof
n += ((xs[(Math.random() * 1000)|0]) instanceof HTMLDivElement)|0;
tagName
n += (xs[(Math.random() * 1000)|0].tagName === `DIV`)|0;
nodeName
n += (xs[(Math.random() * 1000)|0].nodeName === `DIV`)|0;
localName
n += (xs[(Math.random() * 1000)|0].localName === `div`)|0;
__proto__
n += ((xs[(Math.random() * 1000)|0]).__proto__ === HTMLDivElement.prototype)|0;
Open this result on MeasureThat.net