+ 2
Sorting algorithm
I have two objects, Artists and Songs. Artists={ A0: [ "a0", "a1", ...], A1: [ "a21", "a69", ...], ... //255 artists } Songs={ s0:{ ... album: "a0", ... }, s1:{ ... album: "a182", ... }, ... //26,500 songs } I need to find ten artists with most songs. I used this naive approach: let artists=Object.keys(Artists) .map(m=>[ m, Artists[m].map(n=> Object.keys(Songs) .filter(f=>Songs[f].album==n).length) .reduce((mm,n)=>mm+n) ]) .sort((m,n)=>n[1]-m[1]) .slice(0,10); But it takes 70 seconds to execute. Could you come up with a much faster approach? There are 805 albums. Artists object is here http://remi.rf.gd/JS/artists.js Songs object is here http://remi.rf.gd/JS/songs.js https://code.sololearn.com/WK4UfhwmJhrB/?ref=app
5 Answers
+ 1
ODLNT thank you very much for the insight. I have included the Artists and Songs objects on my post. Could you please try executing it again with your machine? Much appreciated.
0
Fernando Moceces based on your post I wrote the code below. I ran the algorithm provided, on my laptop got an average time of 3.9 seconds. I also ran it on bluestack virtual android phone and got an average time of 5.7second.
I tracked the time using window.performance(), not for precision but for general knowledge.
Take a look and let me know if the objects are formatted correctly.
https://code.sololearn.com/WoM17HXrVrND/?ref=app
0
Fernando Moceces
Great now were on the same page.
When I run your algorithm on my laptop with the objects you provided I see an average time of 3.6 seconds.
I was able to come up with something faster using same object and timer on my laptop I get an average time of 0.015 seconds.
I included screenshots and algorithm below
https://imgur.com/Km2uHq3
https://imgur.com/yjeRc8c
https://code.sololearn.com/WI34X9d66qF0
I'm very interested what results you get on your device.
0
ODLNT excuse me but apparently you have your code set private so I can only see snippet of it. Also your google drive links seem to be inaccessible to public as well, they require access request.
0
My bad, I updated my previous post with public links to the screen shots and the code is now public
Hot today
Python help
1 Votes