A Practical Guide to Algorithms with JavaScript

# Big O: Property Lookup

## Bianca Gandolfo

Thumbtack

### Check out a free preview of the full A Practical Guide to Algorithms with JavaScript course

The "Big O: Property Lookup" Lesson is part of the full, A Practical Guide to Algorithms with JavaScript course featured in this preview video. Here's what you'd learn in this lesson:

Bianca walks through a property lookup in an array.length and measures time complexity.

Preview
Close

### Transcript from the "Big O: Property Lookup" Lesson

[00:00:00]
>> Speaker 1: So what is n in this case?
>> Speaker 1: What is the, so n in this case is the length of the string. Then the time complexity is going to be as the string gets longer, how much more work do we need to do?
>> Speaker 1: So when the string is length five, how much work does this expression have to do?

[00:00:40]
>> Speaker 2: But didn't you say return is always gonna be constant?
>> Laney: Well how does it get the length?
>> Speaker 1: Yeah, exactly, Laney. That's the exact point. How does it get the length? We need to understand the length property. So if the length property looped through the length of a string and counted from 0 to n, that would be a linear operation.

[00:01:10]
Luckily, JavaScript's a little bit smarter than that, and if we just keep track of the length as we add items to our array, or we remove items we increment and decrement our length, and so the length is just a property lookup, right? So we have the dot notation, this is just string.

[00:01:28]
Everything's an object in JavaScript and that's why we can do a property lookup and that's gonna be constant time.
>> Speaker 1: And as a result this will always be a constant time algorithm.
>> Speaker 1: Which means if our string length was 1 million it wouldn't be any slower than if it was just 1.

[00:01:56]
>> Speaker 2: Cuz it already knows that.
>> Speaker 1: Yeah, exactly, it doesn't matter.

### Learn Straight from the Experts Who Shape the Modern Web

• In-depth Courses