for-in vs for-of-keys

JavaScript performance comparison

Test case created by greggman

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  const foo = {
  Alabama: 123,
  Alaska: 343,
  Arizona: 203,
  Arkansas: 63,
  California: 235,
  Colorado: 65,
  Connecticut: 23,
  Delaware: 1264,
  Florida: 65,
  Georgia: 23,
  Hawaii: 465,
  Idaho: 7564,
  Illinois: 21,
  Indiana: 245,
  Iowa: 25,
  Kansas: 65,
  Kentucky: 85,
  Louisiana: 236,
  Maine: 7564,
  Maryland: 24,
  Massachusetts: 54,
  Michigan: 464,
  Minnesota: 244,
  Mississippi: 3433,
  Missouri: 342,
  Montana: 364,
  Nebraska: 343,
  Nevada: 766,
  "New Hampshire": 34,
  "New Jersey": 88,
  "New Mexico": 1,
  "New York": -124,
  "North Carolina": 66,
  "North Dakota": 23,
  Ohio: 65,
  Oklahoma: 657,
  Oregon: 35,
  Pennsylvania: 346,
  "Rhode Island": 234,
  "South Carolina": 67,
  "South Dakota": 12,
  Tennessee: 345,
  Texas: 0,
  Utah: 353,
  Vermont: 77,
  Virginia: 12,
  Washington: 656,
  "West Virginia": 217,
  Wisconsin: 167,
  Wyoming: 63,
  };
  

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
for in
let sum = 0;
for (let key in foo) {
  sum += foo[key];
}
pending…
for in hasOwnProperty
let sum = 0;
for (let key in foo) {
  if (foo.hasOwnProperty(key)) {
    sum += foo[key];
  }
}
pending…
for of
let sum = 0;
for (let key of Object.keys(foo)) {
  sum += foo[key];
}
pending…

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

Compare results of other browsers

0 Comments