Return Largest Numbers in Arrays speed?

Hey everyone. I just finished the Basic Algorithm Scripting: Return Largest Numbers in Array
Luckily it passed. However I decided right after to have a look at the hints.

None of the solutions were completely similar to mine, so thumbs up for originality :laughing:

After running my code through a performance benchmark it under performs when comparing it to even the most basic solution:

Here’s my Code

function largestOfFour(arr) {
  // You can do this!
  let newArr = [];
  for (let i = 0; i < arr.length; i++) {
  return newArr;

largestOfFour([[4, 5, 1, 3], [13, 27, 18, 26], [32, 35, 37, 39], [1000, 1001, 857, 1]]); This is the benchmark test. I am hoping that someone can explain to me why this is.

Although your solution is more concise than the basic solution, it is possible that just by using push () and Math.max () you will use infinitesimally more resources. It would be necessary to review the code of these functions. However, in my opinion, when working, it is more recurrent to appeal to the concise.

It would be interesting to compare basic and advanced solutions with one another.

1 Like

How exactly did you test the various solutions?

I tested the various solutions with the link provided ( ). It may be possible that I didn’t conduct the tests correctly as It’s my first time.

A better test would be to create large sub arrays and have more than 4 sub arrays in the array. I tried using the Setup Block area, but get strange results when using that benchmark site. Even though there are strange results (0% for both), then shorter bar maybe indicates the second solution is still faster?

In my example (below), I create 100 sub arrays each with 10,000 elements instead of 4 sub arrays with only 4 elements each.

My assumption is Math.max is sorting the subarray first to capture the maximum value which takes extra time.

Very interesting. Thanks for taking a look at it.
I’m now very intrigued as to how these functions work.
And if in larger scales is it better to go the longer route for reasons like speed?

Thanks again!

Just asking out of interest do you have any past experience on programmjng languages or are u literally a beginner at javascript? Because i struggle alot at algorithms

1 Like

Same boat as you! I’ve been self studying JavaScript a bit over a year now.

Algorithms are usually a massive struggle for me. I’ve done the basic algorithm challenges a looong time ago, and it was hard trying to get my mind to twist and understand things deductively in order to problem solve.

I’ve pretty much forgotten all of the solutions for these challenges, but with the knowledge I’ve accumulated from doing a bit of work and just the consistent studying, I feel like I’m tackling them more effectively! It’s like I worked out & gained a muscle! :muscle:

wow i wish i was at your stage right now but i guess i just have to carry on doing one or more each day because right now the stage i am at even doing one stresses me out fully. how did u go about learning alogorithms btw, did u just fully jump onto them and start breaking them down or did u go through video tutorials or did u just read alot of articles and books?

thanks for the replies btw i really appreciate it

If you got money to spend, I recommend checking out Their videos are really well done and I owe a lot to it. Another thing I recommend is JavaScript: Understanding the Weird Parts

I pretty much jumped straight in the algorithms when I got to them. And it was something I wasn’t very use to, I felt very stupid.

That being said, I think you can get by without spending anything. Just keep doing whatever you can(youtube, documentation, articles, etc.), and don’t stress too much on the algorithms, just start building things & embracing failure. Build whatever you can! And Google A LOT!

I’m still doing it and it’s working great so far!

You can watch the first 3.5 hours of JavaScript: Understanding the Weird Parts for free, i agree it’s pretty good. The book version equivalent of it would probably be Kyle Simpson’s You Don’t Know JS

1 Like