admin管理员组

文章数量:1405543

So, let's say we have this function:

function inc() {
   this.val++;
}

If I do this

var obj = {val: 5};
var incObj = inc.bind(obj);

Will now the inc function be copied in the memory with the given bound value or a single function is stored in the memory and only a reference to the bound value is saved? Where is that reference saved?

I ask this because I want to know if a memory leak could be created making bind calls. (actually I care about garbage collection, not about memory leaks)

So, let's say we have this function:

function inc() {
   this.val++;
}

If I do this

var obj = {val: 5};
var incObj = inc.bind(obj);

Will now the inc function be copied in the memory with the given bound value or a single function is stored in the memory and only a reference to the bound value is saved? Where is that reference saved?

I ask this because I want to know if a memory leak could be created making bind calls. (actually I care about garbage collection, not about memory leaks)

Share Improve this question edited Sep 28, 2016 at 10:58 XCS asked Sep 28, 2016 at 10:49 XCSXCS 28.2k28 gold badges104 silver badges153 bronze badges 15
  • 5 Yes, it's the first line in the documentation -> developer.mozilla/en-US/docs/Web/JavaScript/Reference/… – adeneo Commented Sep 28, 2016 at 10:51
  • But if I log the inc and incObj they both output the exact same thing. Isn't the JS engine smart enough to not make infinite copies of the same thing? – XCS Commented Sep 28, 2016 at 10:53
  • Well no, if a method is supposed to make a brand new copy of something, that's probably what the engine does ? – adeneo Commented Sep 28, 2016 at 10:54
  • @adeneo Actually, looking at that link, it seems it doesn't copy the function, but only wraps a call around it: The bind() function creates a new bound function (BF). A BF is an exotic function object (term from ECMAScript 6) that wraps the original function object. Calling a BF generally results in the execution of its wrapped function. – XCS Commented Sep 28, 2016 at 10:55
  • 1 @evolutionxbox That's probably just because the new bound function has a references to this function and the engine is smart enough to only delete your original function reference :-? – XCS Commented Sep 28, 2016 at 11:03
 |  Show 10 more ments

1 Answer 1

Reset to default 8

Does bind create a new copy of the [underlying] function?

No, it doesn't.

It creates a new function which, when called, invokes the underlying function.

For all practical purposes, bind is:

function bind(fn, thisArg) {
  return function() {
    return fn.apply(thisArg, arguments);
  };
}

As you can see, in no way, shape or form is fn being copied.

In the case of:

var incObj = inc.bind(obj);

Will now the inc function be copied in the memory with the given bound value or a single function is stored in the memory and only a reference to the bound value is saved? Where is that reference saved?

The latter, however it would be more accurate to say "a single new function is stored in memory and within it only a reference to the function on which bind was called".

In other words, inc remains exactly as it was. A new bound function object incObj is created which points internally to inc. In the example above, inc is "stored" by virtue of being closed over by the internal anonymous function. In actuality, inc, meaning a reference to it, is stored within the engine's internal bound function object.

I want to know if a memory leak could be created making bind calls. (actually I care about garbage collection, not about memory leaks)

Merely creating objects is not a "memory leak"; it is only a memory leak if the object will never be GC'd. Simply creating a bound function would never cause a memory leak, because when the bound function goes out of scope, so will the underlying function, so it will be GC'd eventually. If you're actually worried not about memory leaks but about creating objects, which will require more GC and potentially cause GC jag, that is the case for any object you create, not just bound functions.

本文标签: javascriptDoes bind create a new copy of the functionStack Overflow