DEV Community

Code_Regina
Code_Regina

Posted on

Analyzing Performance of Arrays and Objects

                   -The Big O of Objects 
                   -When are Arrays Slow
                   -Big O of Array Methods
Enter fullscreen mode Exit fullscreen mode

The Big O of Objects

The focus will be working with arrays, objects and built-in methods. It is important to understand how objects and arrays work, through the lens of Big O.

Objects are unordered key value pairs.


let instructor = {
    firstName: "Kelly", 
    isInstructor: true, 
    favoriteNumbers: [1, 2, 3, 4]
}

Enter fullscreen mode Exit fullscreen mode

It is good to use objects when your code doesn't not need order. Objects also provide fast access, insertion, and removal.

Common Object Methods


Object.keys
Object.values
Object.entries
hasOwnProperty

Enter fullscreen mode Exit fullscreen mode

When are Arrays Slow

Arrays are ordered list and they are most useful when your code needs to have information in a specific order.


let names = ["Michael", "Melissa", "Andrea"];

let values = [true, {}, [], 2, "awesome"];

Enter fullscreen mode Exit fullscreen mode

Big O of Array Methods

Concat() method is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.

Slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end. The original array will not be modified.

Splice() method changes the contents of an array by removing existing elements and/or adding new elements.

Objects are fast at pretty much everything, but objects have no order. Arrays are great when you need them.

Top comments (0)