Peter Leonov's Blog

Is the object empty?

29 January 2010

How many ways are there to check an object for emptiness?


var hash = {}
JSON.stringify(hash) === '{}'
Object.equals(hash, {}) // for in comparision
Object.keys(hash).length === 0
Object.isEmpty(hash) // for (var k in hash) return false

Lets test…

Object.equals = function (a, b)
{
	for (var k in a)
		if (!(k in b) || a[k] !== b[k])
			return false
	
	for (var k in b)
		if (!(k in a) || a[k] !== b[k])
			return false
	
	return true
}

Object.keys = function (s)
{
	var r = []
	for (var k in s)
		r.push(k)
	return r
}

Object.isEmpty = function (s)
{
	for (var k in s)
		return false
	return true
}

var hash = {},
	count = 10000

console.time('JSON')
for (var i = 0; i < count; i++)
	JSON.stringify(hash) === '{}'
console.timeEnd('JSON')
//=> 61ms

console.time('equals')
for (var i = 0; i < count; i++)
	Object.equals(hash, {})
console.timeEnd('equals')
//=> 49ms

console.time('keys.length')
for (var i = 0; i < count; i++)
	Object.keys(hash).length === 0
console.timeEnd('keys.length')
//=> 44ms

console.time('isEmpty')
for (var i = 0; i < count; i++)
	Object.isEmpty(hash)
console.timeEnd('isEmpty')
//=> 38ms

console.time('isEmpty inlined')
for (var i = 0; i < count; i++)
{
	for (var k in hash)
		false
	true
}
console.timeEnd('isEmpty inlined')
//=> 26ms

Tested on very old Intel Mac (2 GHz) in Firefox 3.6.

Tags:
  • client
  • javascript