Chathura Widanage

@cwidanage

5 Techniques to Iterate Over JavaScript Object Entries and their Performance

June 26th 2018

This post includes 5 different ways for iterating over JavaScript Object entries and a performance comparison of those techniques.

Technique 1 : Object.entries

Object.entries() returns a list of key, value pairs. This list includes only enumerable properties and doesn’t include properties from prototype chain.

Enumerable Properties?
Properties created via simple assignment or via a property initializer

Technique 2 : Object.keys

Object.keys() returns an array of object keys. However, this function returns only enumerable properties.

Technique 3 : Object.values

Object.values() returns an array of object property values. This function returns values of enumerable properties only.

Technique 4 : for…in loop

for…in loop can be used to iterate over enumerable properties of JavaScript objects. This loop includes inherited properties from prototype chain.

Technique 5 : Object.getOwnPropertyNames

Object.getOwnPropertyNames returns all the properties of an object including non enumerable properties. The result set will also include properties inherited from prototype chain.

Performance Comparison

In order to compare the performance of each of above techniques, following script was executed for 1000 objects having 1 million properties in each.

Results

Performance test results

Chart below gives a better comparison overview of the techniques.

Performance comparison of JavaScript Object iteration techniques

Based on above results, the winner or the fastest technique to iterate over JavaScript Object entries is for…in.

Call To Action

  • Clap. Appreciate and let others find this article.
  • Comment. Share your views on this article.
  • Follow me. Chathura Widanage to receive updates on articles like this.
  • Keep in touch. LinkedIn, Twitter

Originally published at gists.cwidanage.com.

More by Chathura Widanage

More Related Stories