Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36
Chrome 95
Windows
Desktop
3 years ago
Test name Executions per second
isEmpty1 2611.9 Ops/sec
isEmpty2 7837.2 Ops/sec
Script Preparation code:
AخA
 
function isEmpty1(obj) {
    return (Object.keys(obj).length === 0) && obj.constructor === Object;
}
function isEmpty2(obj) {
    for (key in obj) {
      if (Object.prototype.hasOwnProperty.call(obj, key)) {
        return false;
      }
    }
    return true;
}
var a = {};
Tests:
  • isEmpty1

     
    var count = 1000;
    for (i=0;i<count;i++) isEmpty1(a);
  • isEmpty2

     
    var count = 1000;
    for (i=0;i<count;i++) isEmpty2(a);