Skip to content

Instantly share code, notes, and snippets.

@DmytroVasin
Last active April 5, 2018 05:37
Show Gist options
  • Save DmytroVasin/670d3a8d970555d988717e61bbb5ea0e to your computer and use it in GitHub Desktop.
Save DmytroVasin/670d3a8d970555d988717e61bbb5ea0e to your computer and use it in GitHub Desktop.
Pulse of Garbage Collector in V8. See more: https://www.youtube.com/watch?v=ZAJmJmKWNPw
getFakeHistory = function(){
return (new Array(20000).join('a'))
}
function Record(id, description) {
this.id = id;
this.description = description;
}
# We need that function to prevent V8 optimisation
Record.prototype.doMagic = function(){
return this.id + 1
}
var sum = 0;
for (var i = 0; i < 10000; i++){
console.time('create')
var record = new Record(i, getFakeHistory());
sum += record.doMagic();
console.timeEnd('create')
}
# =>
-> 0.03369140625ms
-> 0.033935546875ms
-> 0.034912109375ms
-> 0.0341796875ms
-> 0.03271484375ms
-> 0.046142578125ms
+> 0.26611328125ms
-> 0.0380859375ms
-> 0.034912109375ms
-> 0.03466796875ms
-> 0.033935546875ms
-> 0.03369140625ms
-> 0.035888671875ms
-> 0.034912109375ms
+> 0.242919921875ms
-> 0.034912109375ms
-> 0.03369140625ms
-> 0.03515625ms
-> 0.032958984375ms
-> 0.033935546875ms
-> 0.034912109375ms
-> 0.031982421875ms
+> 0.241943359375ms
-> 0.032958984375ms
-> 0.0322265625ms
Solution is to allocate object before the loop and store them inside global variable,
So you restrict that object for GC circle _
for (var i = 0; i < 10000; i++) {
records[i] = new Record()
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment