isEmpty

Benchmark created by Sindre Sorhus on


Description

Check to see what's the fastest isEmpty method.

Preparation HTML

<script>
  var a = {};
  
  var b = {
   one: 1,
   two: 2,
   three: "3"
  };
  
  
  // original
  
  function isEmpty(obj) {
   for (var key in obj) {
    return false;
   }
   return true;
  }
  
  
  // only in newer browsers
  
  function isEmpty2(obj) {
   return ( !! Object.keys(obj).length);
  }
  
  
  // only in Firefox
  
  function isEmpty3(obj) {
   return !obj.toSource()[4];
  }
  
  
  // only in Firefox
  
  function isEmpty4(obj) {
   return obj.toSource() === '({})';
  }
  
  
  // only in newer browsers
  
  function isEmpty5(obj) {
   return !Object.getOwnPropertyNames(obj)[0];
  }
  
  
  // only in newer browsers
  
  function isEmpty6(obj) {
   return !Object.getOwnPropertyNames(obj).length;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
isEmpty original - a
isEmpty(a);
ready
isEmpty original - b
isEmpty(b);
ready
isEmpty2 - a
isEmpty2(a);
ready
isEmpty2 - b
isEmpty2(b);
ready
isEmpty3 - a
isEmpty3(a);
ready
isEmpty3 - b
isEmpty3(b);
ready
isEmpty4 - a
isEmpty4(a);
ready
isEmpty4 - b
isEmpty4(b);
ready
isEmpty5 - a
isEmpty5(a);
ready
isEmpty5 - b
isEmpty5(b);
ready
isEmpty6 - a
isEmpty6(a);
ready
isEmpty6 - b
isEmpty6(b);
ready

Revisions

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

  • Revision 1: published by Sindre Sorhus on