5 Techniques to Iterate Over JavaScript Object Entries and their Performance by@cwidanage

5 Techniques to Iterate Over JavaScript Object Entries and their Performance

image
Chathura Widanage HackerNoon profile picture

Chathura Widanage

image

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

image

Technique 2 : Object.keys

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

image

Technique 3 : Object.values

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

image

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.

image

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.

image

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.

image

Results

image

Performance test results

Chart below gives a better comparison overview of the techniques.

image

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.

Comments

Signup or Login to Join the Discussion

Tags

Related Stories