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');

//write out your function
const functionName = () => {
   //code goes here
//time your function
const startTimer = performance.now();
const endTimer = performance.now();
//log how many milliseconds it took your function to execute
console.log(`This function took ${endTimer - startTimer} milliseconds`);

You can place the start and end of your timer anywhere, so this can also be a handy way to test sections of your function to see what’s dragging you down.Let’s practice this with an actual example, solving the level “medium” Matching Strings problem from HackerRank using two different approaches.HackerRank’s Matching Strings problem asks us to create a function which takes two parameters: “strings”, an array of strings to search, and “queries”, an array of queries. With these two parameters, we are asked to create a function that returns an array of integers representing the frequency of occurrence of each query in the strings array.

#software-development #code #software-engineering #technology #javascript

Experiments with Time Complexity in JavaScript
1.60 GEEK