nodeType vs duck typing

Benchmark created by Hubert SABLONNIERE on


Description

Is it faster to test node type or to verify if method exists

Setup

var div = document.createElement('div'),
        divBool,
        textNode,
        textNodeBool;
    
    div.innerHTML = 'text';
    textNode = div.firstChild;

Test runner

Ready to run.

Testing in
TestOps/sec
nodeType
divBool = div.nodeType === 1;
textNodeBool = textNode.nodeType === 1;
ready
duck typing
divBool = div.getElementsByTagName !== undefined;
textNodeBool = textNode.nodeType !== undefined;
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Hubert SABLONNIERE on