Ranter
Join devRant
Do all the things like
++ or -- rants, post your own rants, comment on others' rants and build your customized dev avatar
Sign Up
Pipeless API
From the creators of devRant, Pipeless lets you power real-time personalized recommendations and activity feeds using a simple API
Learn More
Comments
-
Wondering, what's the best way to write this?
Switch?
Simpler ifs?
Counting digits?
? -
@No-one Yeah. When doing it with ints its also possible by dividing by 10 until value is 0. Dont forget to count times until its 0.
-
@No-one Counting digits is a simple one liner but is it the best way (performance wise)?
-
Well...
https://jsperf.com/170313
OS: Win10 Pro Insider 15055
On FF Dev 54.0.0 [12/03/17] (heavily modified profile with extensions and 100+ tabs) case 1 (simple ifs) wins with 800 million ops.
Cases 3-6 (toString, log10) are around 31-36 million ops.
Cases 7, 8 (continuous division by 10) 483 million ops best case, 6,5 million ops worst case.
Cases 9, 10 (else if) are marginally worse than simple ifs.
On Chrome Dev 58.0.3029 (relatively clean, a few extensions, only tab) I am getting a lot less ops (about 20 times less) than FF, not sure why.
Cases 1 and 9 (best case ifs, else ifs) are on top with 4,4 million ops.
Cases 2 and 10 (worst case ifs, else ifs) are at 2,3 million ops.
Cases 3-6 (toString, log10) are at 3 to 3, 5 million ops.
Cases 7, 8 (continuous division by 10) 4,3 million ops best case, 1,3 million ops worst case.
Edge follows same pattern as Chrome but with much higher ops.
Feedback?
PS: I'm not a dev, excuse any mistakes. -
Can anyone explain the Chrome results?
I thought their engine was much faster than Firefox's. -
Sleziak7118yYou might be laughing at this, but this is actually the most efficient approach if you're working with very CPU constrained applications (embedded programming).
seems legit xD
undefined