-2

In javascript, is there a speed advantage between the multiplication operator and the division operator? As an example...

var foo = bar * 0.01;
var foo = bar / 100;

foo is the same for both, but which statement returns the value of foo the fastest? I know this may be an incredibly small difference, however, when loop processing large amounts of data it could make a bigger difference than realized, which would then make a difference in how I construct equations to facilitate the processing.

JJJ
  • 32,902
  • 20
  • 89
  • 102
Epiphany
  • 1,886
  • 1
  • 20
  • 14
  • 4
    Can't you just test it yourself? – KSFT Feb 28 '15 at 16:21
  • @KSFT. Yes I could... and I may have to, but that defeats the purpose of this post, which is to make this answer available to the community. – Epiphany Feb 28 '15 at 16:24
  • 1
    This should depend much more on the processor and its floating point capabilities than on the JavaScript engine or even the language itself. The result of any test is much too specific, and not generaliseable. – Bergi Feb 28 '15 at 16:25
  • You know if you plan on doing your own research there's a checkbox called "Answer your own question" on the ask page. – Matti Virkkunen Feb 28 '15 at 16:28
  • @KSFT. First, that is not an answer, it's a question, so I don't understand why anyone would upvote you. Second, understanding how operators consume system resources should be fundemental to good programming practices, and therefore, this is a prefectly legitimate question. And Third, I could care less about the votes, folks... I want an authoritative answer to the question, not votes. – Epiphany Feb 28 '15 at 16:38
  • 1
    Of course it isn't an answer; that's why it isn't an answer. – KSFT Feb 28 '15 at 16:40
  • I assume they upvoted me because they agreed with me. SO questions are supposed to show research effort, which this does not. I never claimed it wasn't a legitimate question; it would be fine if you had attempted to answer it yourself. About your third point...you're the only one who brought up votes with "I don't understand why anyone would upvote you" – KSFT Feb 28 '15 at 16:42
  • 1
    @KSFT. All I gotta say is you must have buddies, because you sure didn't get those upvotes for anything to do with the OP. – Epiphany Feb 28 '15 at 16:43
  • 2
    It was something to do with my comment, not the question. That's why they upvoted my comment, not the question. – KSFT Feb 28 '15 at 16:46
  • 1
    Possible duplicate of [JavaScript Performance - Divide or Multiply? / vs \*](https://stackoverflow.com/questions/10535605/javascript-performance-divide-or-multiply-vs) – deblocker Oct 15 '17 at 20:30

2 Answers2

1

I would say, it depends on the implementation. I would just make an own test somehow or try to google it.

For most machines, multiplications is faster, but the raw CPU speed is not decisive when it comes to scripting languages. Even when the implementation is the same, the execution time for one or the other will not differ so much, since the overhead of scripting languages is normally much bigger.

Normally the difference between different operations is so small, that it is not worth it to think about it. When you must, you probably are using the wrong language, anyhow.

Juergen
  • 12,378
  • 7
  • 39
  • 55
  • I agree, but it is in my nature not to be able to stop thinking about something until I understand all that it entails completely! LOL – Epiphany Feb 28 '15 at 17:42
0

In computer systems, the only basic operators are + (adders) and (*) multipliers. You either add (negative or positive) numbers or rotate numbers (either left or right for multiplication and division respectively). You should be able to work it out whether multiplication or division takes longer...

*btw...unless I am wrong, your question has nothing to do with javascript. Javascript is an interpreted language with engines such as spidermonkey or rhino....

FYI You should read this - directly from the main people....to have better insight of "what" might be happening.

ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
  • @ha9u63ar. Would there really be any variation between different javascript engines on something so fundamental to the language? I am even more intrigued about the answer to this now, and would really like to know! – Epiphany Feb 28 '15 at 17:38
  • @Epiphany I mentioned engines just as a part of my answer. The objective is to let OP know that his question is actually the same if it was asked for Python, C/C++, Java...or whatever. If you do a benchmark between Python/C++/Java/Perl/JavaScript for adder/multiplier cycles......the difference is negligible. In Real-time systems when 0.05 miliseconds make a lot of difference, you actually go about doing those speed-based optimisations (by botching assembly code)..... – ha9u63a7 Feb 28 '15 at 18:16
  • @ha9u63ar. But unlike other languages, javascript is effected by the current state and activity of the browser, i.e. sharing time with other processes running external to my own javascript in other pages on other tabs invoked by the user. So I imagine their would be no black and white answer to this, as there is an undefined variable... the end user. – Epiphany Feb 28 '15 at 18:45
  • 1
    " javascript is effected by the current state and activity of the browser, i.e. sharing time with other processes......" how is this relevant? Division and multiplication are arithmetic operation and inter-process communication is totally different (i.e. what you speculating in your last comment). Also, the operation you have shown here is a floating point operation. Optimised multiplication/division will be based on rotation to left or right. There is no black and white answer - because your understanding about these arithmetic ops. may need to be clearer.... – ha9u63a7 Feb 28 '15 at 22:45