3

A simplified example:

function shorten(string) {
  return string.slice(0, 3);
}

const today = "Friday";
if (shorten(today) === "Fri") {
  console.log("Oh yeah it's " + shorten(today));
}

shorten(today) is called twice here, which makes me feel bad. I believe we all run into this situation every day, and what we do is store the the value of shorten(today) in a variable first, then use that variable twice.

My question is: are modern JS engines smart enough so that I actually don't need to worry about it?

ZYinMD
  • 3,602
  • 1
  • 23
  • 32
  • _"are the modern JS engines smart enough so that I actually don't need to worry about it?"_ No. No engine can reliably predict the return value of a function call. What are you trying to achieve? – guest271314 Feb 08 '19 at 22:54
  • Modern JS Engines are indeed smart & fast. But it's not going to convert a function in a var for you,. It will execute it again, the second time might be faster due to certain optimizations etc. But for a function that just does `string.slice(0, 3)`, I've a feeling you won't be hitting any performance barries any time soon. – Keith Feb 08 '19 at 22:56
  • 1
    Here is a counter example that shows why things are not necessarily easy to optimize: `shorten(today); String.prototype.slice = () => 'foo'; shorten(today)` – Felix Kling Feb 08 '19 at 23:36
  • @FelixKling And, but not limited to, `function shorten(string) { try { return string.slice(0, 3); } catch(e){ } finally { shorten = function(string) { while (true) { shorten(string) } } } } const today = "Friday"; if (shorten(today) === "Fri") { console.log("Oh yeah it's " + shorten(today)); }` – guest271314 Feb 08 '19 at 23:50

2 Answers2

4

If you run shorten multiple times, the V8 engine has a JIT compiler that will optimize that piece of code so it runs faster the next time.

When it runs into the same function call for the 2nd time, maybe it's able to realize it has just did the same calculation, and still have the result in memory

What you described is known as memoization, and V8 doesn't do that. However, there are libraries out there (e.g. fast-memoize) that does.

But you best bet is still to store the result of the computation in a variable and reference it.

d4nyll
  • 11,811
  • 6
  • 54
  • 68
1

When I execute a simple JS function twice in a row, does it cost twice the computing power?

Yes. Consider Why is using a loop to iterate from start of array to end faster than iterating both start to end and end to start?

are the modern JS engines smart enough so that I actually don't need to worry about it?

No. No engine can reliably predict the return value of a JavaScript function call. See Has it been mathematically proven that antivirus can't detect all viruses? Can a regular expression be crafted which determines the return type of a function?

guest271314
  • 1
  • 15
  • 104
  • 177
  • The engine doesn't necessarily need to "predict the return value". When it runs into the same function call for the 2nd time, maybe it's able to realize it has just did the same calculation, and still have the result in memory, so it just uses the previous result. I'm not saying it's able to do so, but the OP's question is not about prediction. – ZYinMD Feb 08 '19 at 23:08
  • @ZYinMD _"so that I actually don't need to worry about it?"_ Implies absolute reliance on the architecture without any bugs. In essence a _"an idealized computer with unlimited resources"_, where still _"1. If a (logical or axiomatic formal) system is consistent, it cannot be complete. 2. The consistency of axioms cannot be proved within their own system"_ and _"Thus there will always be at least one true but unprovable statement."_ apply; see _"On Formally Undecidable Propositions of "Principia Mathematica" and Related Systems"_ by Kurt Gödel published in 1931 – guest271314 Feb 08 '19 at 23:10
  • @ZYinMD See [How do I check if a JavaScript function returns a Promise?](https://stackoverflow.com/questions/43416214/) – guest271314 Feb 08 '19 at 23:37