explicit-exist (v3)

Revision 3 of this benchmark created by rmunson on


Description

checky check

Preparation HTML

<script type="text/javascript">
var exists = function(prop, path, falseNull) {
        if (typeof falseNull === "undefined" || falseNull === null) {
            falseNull = false;
        };

        if ((typeof prop === "undefined" || prop === null)) {
            return false;
        } else if (falseNull && prop === false) {
            return false;
        }

        if (typeof path !== "undefined") {
            if (path.indexOf(",") >= 0 || typeof path !== "string") { return checkSet(); }
            else { return checkDeep(); }

        } else {
            return !(typeof prop === "undefined" || prop === null || (falseNull && prop === false));
        }

        function checkDeep() {
            var vals = path.split("."),
                obj = prop;
            for (var i = 0, len = vals.length; i < len; i++) {
                if (typeof obj[vals[i]] === "undefined" || obj[vals[i]] === null) {
                    return false
                } else if (falseNull && obj[vals[i]] === false) {
                    return false;
                }
                obj = obj[vals[i]];
            }
            return true;
        }

        function checkSet() {
            var vals = (typeof path === "string") ? path.split(",") : [].concat(path),
                obj = prop;

            for (var i = 0, l = vals.length; i < l; i++) {
                if (typeof obj[vals[i]] === "undefined" || obj[vals[i]] === null) {
                    return false;
                } else if (falseNull && obj[vals[i]] === false) {
                    return false;
                }
            }

            return true;
        }
    };

var exister = (function() {
   var UNDEF   = "undefined",
        STRING  = "string",
        COMMA   = ",",
        DOT     = ".";

    /**
     * Confirm an object path exists
     * @param  {object}     obj       object to test agains
     * @param  {string}     path      Dot delimited path
     * @param  {boolean}    falseNull Should false be considered null
     * @return {boolean}              True if full path exist
     */
   function checkDeep(obj, path, falseNull){
        var vals = path.indexOf(DOT) != -1 ? path.split(DOT) : [path],
            i = 0,
            val = obj,
            len = vals.length;

        for (; i < len; i++) {
            val = val[vals[i]];
            if(nullOrUndef(val,falseNull)){
                return false
            }
        }
        return true;
    }


    /**
     * Confirm a set of properties exist on an object
     * @param  {object}     obj        object to test agains
     * @param  {string}     props      Comma delimited set of properties
     * @param  {boolean}    falseNull  Should false be considered null
     * @return {boolean}               True if all properties exist 
     */
    function checkSet(obj,props,falseNull) {
        var vals = (typeof props === STRING) ? (props.indexOf(COMMA) != -1 ? props.split(COMMA) : [props]): [].concat(props),
            i = 0,
            len = vals.length;

        for (; i < len; i++) {
            if(!checkDeep(obj,vals[i],falseNull)){
                return false
            }
        }
        return true;
    }
    /**
     * Check if a object is undefined or null
     * @param  {mixed}  thing     Object to test
     * @param  {boolean}falseNull Should false be considered null
     * @return {boolean}          True if false or null
     */
    function nullOrUndef(thing,falseNull){
        return (typeof thing === UNDEF  || thing === null || (falseNull && thing === false));
    }

    return function(prop, path, falseNull) {
        falseNull = falseNull===true;
        var nope = nullOrUndef(prop,falseNull);

        if(nope){
            return false
        }

        if (typeof path !== UNDEF){
                return checkSet(prop, path, falseNull); 
        }
        return !nope;
    }
})();

var undef,
    nullDef = null,
    eObject = {},
    eObjectProp = {property : 'yeah', path : { to : {thing : "thing"}}};
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
old exists
exists(undef)
exists(nullDef)
exists(eObject)
exists(eObject,"property")
exists(eObject,"path.to.thing")
ready
new exists
exister(undef)
exister(nullDef)
exister(eObject)
exister(eObject,"property")
exister(eObject,"path.to.thing")
ready

Revisions

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

  • Revision 3: published by rmunson on