If you are interviewing for your first software engineering role after finding this field through an untraditional path, like myself, you will soon reach the point where you need a solid understanding of Big O notation and performance. With Big O notation, understanding goes many layers deep. There is the surface level understanding of “Oh yes, that’s the one with all the O’s and n’s”, the deeper academic understanding, and a more concrete understanding of how the concepts presented in Big O actually impact your code.If you’re looking for a deeper academic understanding of Big O and time complexity, this isn’t the article you want. There’s plenty of fantastic resources out there that cover this very topic. In this article, I’ll instead provide a simple do-it-yourself experiment that helped me to develop a more concrete understanding of measuring performance by timing code with different input sizes using performance.now().

Interview Cake gives a great definition of Big O notation:“With big O notation, we express the runtime in terms of — brace yourself — how quickly it grows relative to the input as the input grows arbitrarily large.”The reason I love Interview Cake’s explanation of Big O is that it also cautions against ONLY looking at Big O when analyzing performance.“Big O ignores constants, but sometimes constants matter. If we have a script that takes 5 hours to run, an optimization that divides that runtime by 5 might not affect big O, but it still saves you 4 hours of waiting… A great engineer (startup or otherwise) knows how to strike the right balance between runtime, space, implementation time, maintainability, and readability.”

Now that you have the definition and link to a great resource to learn more, how can we run experiments to test these concepts on actual code? This is where performance.now() comes in when working with JavaScript. With performance.now(), we can create a simple timer to measure how many milliseconds our code takes to execute.First, let’s look at the basic set up:

``````//make sure you're able to access performance.now()
const {performance} = require('perf_hooks');

const functionName = () => {
//code goes here
}