What is the fastest way to check if an object is empty or not?

Is there a faster and better way than this:

function count_obj(obj){
    var i = 0;
    for(var key in obj){
        ++i;
    }

    return i;
}

23 s
23

For ECMAScript5 (not supported in all browsers yet though), you can use:

Object.keys(obj).length === 0

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *