Type checking

Benchmark created on


Setup

/**
 * Tells you the constructor (so basically the type) of the passed value
 * @param {*} value 
 * @returns **String**, with first letter capitalized (e.g. "String","Object",...)
 * @see [Erisan Olasheni](https://stackoverflow.com/a/51458052/20015615)
 * @author bye-csavier
*/
function getType(x) {
    /** 
     * about void 0 --> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/void#description
     * about (x!==x) --> https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/isNaN#description
     * about (x===x/0) --> https://stackoverflow.com/a/20608099
    */
    return  (x === null)? "Null" : 
            (x === void 0)? "Undefined" : 
            (x!==x)? "NaN" :
            (x===1/0 || x===-1/0)? "Infinity" :
            x.constructor.name;
}

let a = {ok:3}
let b;

Test runner

Ready to run.

Testing in
TestOps/sec
getType() fn
b = getType(a)
ready
typeof
b = typeof a
ready
direct use of x.constructor.name
b = a.constructor.name
ready

Revisions

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