检查对象是否为空的最快方法是什么?

有没有比这更快更好的方法:

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

    return i;
}

当前回答

编辑:请注意,您可能应该使用ES5解决方案而不是这个,因为ES5支持现在已经广泛使用。但它仍然适用于jQuery。


简单和跨浏览器的方法是使用jQuery.isEmptyObject:

if ($.isEmptyObject(obj))
{
    // do something
}

更多:http://api.jquery.com/jQuery.isEmptyObject/

但是你需要jquery。

其他回答

这里有一个好方法

function isEmpty(obj) {
  if (Array.isArray(obj)) {
    return obj.length === 0;
  } else if (typeof obj === 'object') {
    for (var i in obj) {
      return false;
    }
    return true;
  } else {
    return !obj;
  }
}

编辑:请注意,您可能应该使用ES5解决方案而不是这个,因为ES5支持现在已经广泛使用。但它仍然适用于jQuery。


简单和跨浏览器的方法是使用jQuery.isEmptyObject:

if ($.isEmptyObject(obj))
{
    // do something
}

更多:http://api.jquery.com/jQuery.isEmptyObject/

但是你需要jquery。

情况有多糟?

function(obj){
    for(var key in obj){
        return false; // not empty
    }

    return true; // empty
}

我想你说的空是指“没有自己的属性”。

// Speed up calls to hasOwnProperty
var hasOwnProperty = Object.prototype.hasOwnProperty;

function isEmpty(obj) {

    // null and undefined are "empty"
    if (obj == null) return true;

    // Assume if it has a length property with a non-zero value
    // that that property is correct.
    if (obj.length > 0)    return false;
    if (obj.length === 0)  return true;

    // If it isn't an object at this point
    // it is empty, but it can't be anything *but* empty
    // Is it empty?  Depends on your application.
    if (typeof obj !== "object") return true;

    // Otherwise, does it have any properties of its own?
    // Note that this doesn't handle
    // toString and valueOf enumeration bugs in IE < 9
    for (var key in obj) {
        if (hasOwnProperty.call(obj, key)) return false;
    }

    return true;
}

例子:

isEmpty(""), // true
isEmpty(33), // true (arguably could be a TypeError)
isEmpty([]), // true
isEmpty({}), // true
isEmpty({length: 0, custom_property: []}), // true

isEmpty("Hello"), // false
isEmpty([1,2,3]), // false
isEmpty({test: 1}), // false
isEmpty({length: 3, custom_property: [1,2,3]}) // false

如果你只需要处理ECMAScript5浏览器,你可以使用Object。getOwnPropertyNames而不是hasOwnProperty循环:

if (Object.getOwnPropertyNames(obj).length > 0) return false;

这将确保即使对象只有不可枚举的属性,isEmpty仍然会给您正确的结果。

我修改了肖恩·维埃拉的代码来满足我的需要。Null和undefined根本不算作对象,数字、布尔值和空字符串返回false。

'use strict'; // Speed up calls to hasOwnProperty var hasOwnProperty = Object.prototype.hasOwnProperty; var isObjectEmpty = function(obj) { // null and undefined are not empty if (obj == null) return false; if(obj === false) return false; if(obj === true) return false; if(obj === "") return false; if(typeof obj === "number") { return false; } // Assume if it has a length property with a non-zero value // that that property is correct. if (obj.length > 0) return false; if (obj.length === 0) return true; // Otherwise, does it have any properties of its own? // Note that this doesn't handle // toString and valueOf enumeration bugs in IE < 9 for (var key in obj) { if (hasOwnProperty.call(obj, key)) return false; } return true; }; exports.isObjectEmpty = isObjectEmpty;