Run details:
Mozilla/5.0 (iPhone; CPU iPhone OS 15_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.5 Mobile/15E148 Safari/604.1
Mobile Safari 15
iOS 15.5
Mobile
2 years ago
Test name Executions per second
Object.entries 91474.5 Ops/sec
Object.keys 71464.7 Ops/sec
Object.keys with extra array 39287.6 Ops/sec
Object.entries without array 97409.0 Ops/sec
Object.keys as separate array with for loop 79458.3 Ops/sec
Script Preparation code:
x
 
function makeid() {
  var text = "";
  var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
  for (var i = 0; i < 5; i++)
    text += possible.charAt(Math.floor(Math.random() * possible.length));
  return text;
}
window.parentObj = {};
for (let i = 0; i < 100; i++) {
    window.parentObj[makeid()] = makeid();
}
Tests:
  • Object.entries

     
    const newObj = {};
    Object.entries(window.parentObj).forEach(([k, v], i) => {
      if ((i % 2) === 0) {
        newObj[k] = v;    
      }
    });
  • Object.keys

     
    const newObj = {};
    Object.keys(window.parentObj).forEach((k, i) => {
      if ((i % 2) === 0) {
        newObj[k] = window.parentObj[k];    
      }
    });
  • Object.keys with extra array

     
    const newObj = {};
    Object.keys(window.parentObj).forEach((k, i) => {
      const [extraK, v] = [k, window.parentObj[k]]
      if ((i % 2) === 0) {
        newObj[extraK] = v;    
      }
    });
  • Object.entries without array

     
    const newObj = {};
    Object.entries(window.parentObj).forEach((keyAndVal, i) => {
      if ((i % 2) === 0) {
        newObj[keyAndVal[0]] = keyAndVal[1];    
      }
    });
  • Object.keys as separate array with for loop

     
    const newObj = {};
    const keys = Object.keys(window.parentObj)
    for (let i = 0; i < keys.length; i++) {
      const k = keys[i]
      if ((i % 2) === 0) {
        newObj[k] = window.parentObj[k];    
      }
    };