Comment

Comment by isntitvacant

In both examples, the function is only allocated once, no matter how it's instantiated.

Where you have to be careful is if you have a function that's being called repeatedly and it contains a function instantiation -- that function will be reallocated each time.

   [1,2,3].forEach(function(x) {
     return (function(a, b) { return a * b })(x, 2)
   })
Will repeatedly allocate that inner multiplication function, vs:

  mul = function(a, b) { return a * b }
  ;[1,2,3].forEach(function(x) {
    return mul(x, 2)
  })
Will only allocate that multiplication function once.
parent isntitvacant 14 years ago

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton