r/ProgrammerHumor • • Jun 21 '18

Thanks Brendan for giving us the Javascript

Post image
3.1k Upvotes

302 comments sorted by

View all comments

Show parent comment

1

u/SasDerGrosse Jun 21 '18

Doesn't that mean JS is slower in math-related functions?

Sorry if I misunderstood, I'm just a beginner programmer.

6

u/once-and-again ☣️ Jun 21 '18

In theory, yes, it would be slower for things that only need integers. In practice, there are so many other things that slow JS execution down so much more that this probably isn't a bottleneck worth worrying about.

If you're doing serious number-crunching, you don't use JS — you use something compiled to native code, possibly with the sticky parts written in hand-optimized assembler.

1

u/SasDerGrosse Jun 22 '18

Oh alright, thanks for letting me know!

Another question: do people still write assembly code nowadays? I thought compilers do most of the necessary optimisations automatically.

2

u/once-and-again ☣️ Jun 22 '18

Not often, but it does happen. There are a rare few times when you know more about your code than you can explain to the compiler; there are a rare few times within that when that actually matters enough to bother with writing assembler.

Usually this isn't just for speed's sake when number-crunching. (That does happen, but only about once in a month's worth of blue moons.) The case I've most commonly seen is where you need to avoid a possible side-channel attack in your cryptography code — usually timing differences on different inputs — and so have to hand-write it in assembler to prevent dangerous optimizations while retaining the safer ones.

This is all library stuff, though. Application logic is basically only written in assembler if you're showing off.

0

u/[deleted] Jun 21 '18

[deleted] — view deleted comment

1

u/SirButcher Jun 21 '18

Which result in a million of parseInt() when something must be correct.

1

u/caerphoto Jun 21 '18

Huh? parseInt is for turning strings into numbers.

You can use the various Math methods (round, floor, etc.) or just do 123.456 | 0 if you need performance.

In most cases JS is plenty fast enough anyway thanks to JIT compilation and various optimisations.

1

u/KingTomenI Jun 22 '18

way too many toFixed ... but that returns a string so then you still have to coerce it to a number. so inefficient and so much repetitive cruft that should be unnecessary in a properly designed language

1

u/KingTomenI Jun 22 '18

the real problem is that the math operators actually return incorrect results .... it's not like anyone would do anything in js relating to money or that people would be angry about software being imprecise with their money