Arrays V/S Objects Performance Analysis

Adityavardhan.Nagamalli
3 min readNov 7, 2019

Built-In Data Structures in JavaScript

Through the lens of Big O

What are the easy and fast things that we are doing to an array and objects. Whats the method that might be slower than we expect.. let’s dive into the topic how arrays and objects will perform.

Now that we’ve covered Big O

Let’s spend a couple minutes analyzing the things we do all the time in JS: Working with Arrays, Objects, and built-in methods.

OBJECTIVES

  1. Understand how Objects and Arrays work, through the lens of Big O
  2. Explain why adding elements to the beginning of an array is costly.
  3. Compare and contrast the runtime for arrays and objects, as well as built-in methods

OBJECTS

  1. Let’s begin by talking about objects, talking about them through the lens of Big O and performance.
  2. Hopefully, you are familiar with Big O , if not no problem you can learn here

3. Objects are unordered data structures, and everything is stored in Key/Value pairs.

When to use Objects

  1. Objects work well when you don’t need order.
  2. When you need fast access / insertion and removal
  3. When you don’t need any ordering, objects are excellent choice

Big O of Objects

Insertion — O(1)

Removal — O(1)

Searching — O(N)

Access — O(1)

Big O of Object Methods

Object.keys — O(N)

Object.values — O(N)

Object.entries — O(N)

hasOwnProperty — O(1)

ARRAYS

Let’s talk about Arrays , how do they compare to objects. If you are not familiar with Arrays . first go and learn about arrays its a very useful and simple concept.

  1. Arrays are Ordered lists
Arrays

When to use Arrays

  1. When you need order
  2. When you need fast access / insertion and removal

Big O of Arrays

Insertion — It depends..

Insertion in arrays is depends on how you insert the data to an array, you can insert data from beginning of an array or ending of an array by using shift and push . Once, you did that we have to re-index every element in the array. so, it depends on which way you choose to insert

Removal — It depends..

Removal in arrays is depends on how you remove the value from an array by using pop or unshift. Once, you did that we have to re-index every element in the array. so, it depends on which way you choose to remove.

Searching — O(N)

Access — O(1)

Big O of Array Operations

push — O(1)

pop — O(1)

shift — O(N)

unshift — O(N)

concat — O(N)

slice — O(N)

splice — O(N)

sort — O(N * log N)

forEach/map/filter/reduce/etc. -O(N)

Comparison Objects/Arrays

Objects are super fast when you don’t bother about order. Arrays are great when you avoid using shift and unshift operations . otherwise both are super cool.

Thanks for your time, i am pretty much happier about you , you learnt a cool stuff about objects vs arrays . Any corrections or suggestions or improvements feel free to comment.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Adityavardhan.Nagamalli
Adityavardhan.Nagamalli

Written by Adityavardhan.Nagamalli

worked as Software Engineer ,Ex-Cognitive Innovations, AWS Cloud Engineer, AWS-CLF-C01, GCP Associate, GCP DevOps

No responses yet

Write a response