DEV Community

Discussion on: Solving Puzzles With High-Performance JavaScript

Collapse
 
healeycodes profile image
Andrew Healey

Thanks for sharing this cool solution! I actually found this was slower on LeetCode than the post’s iterative solution. Crazy right? They must use low values of N in the test cases. I wonder if it’s quicker in other languages πŸ€”.

Collapse
 
stefant123 profile image
StefanT123

Hmm, I've run some benchmark tests (I ran them online on Chrome so I don't know how reliable they are) and I got two different results on two different sites. On the first site (jsben.ch/) it seems that for smaller numbers, the iterative way is faster, the real power of the pure math function is when the N >= 200. While on the other site (perf.zone/), the math function is way faster even for smaller numbers. I got this results on the perf.zone/quick:

  • for N = 20, math -> 705,738,258 op/second; iterative -> 66,518,777 op/second
  • for N = 50, math -> 725,140,230 op/second; iterative -> 16,581,587 op/second
  • for N = 100, math -> 700,447,370 op/second; iterative -> 5,875,773 op/second
  • for N = 200, math -> 706,141,355 op/second; iterative -> 2,562,412 op/second

But anyway I wonder what time complexity does Javascript Math functions have πŸ€”.

Thread Thread
 
healeycodes profile image
Andrew Healey

The variance is very interesting. I wonder if Chrome throttles certain calculations.

Thread Thread
 
stefant123 profile image
StefanT123

I think it does.