-
-
Save DronRathore/88805853b40d7001b651 to your computer and use it in GitHub Desktop.
var globalTime = new Date().getTime(); | |
var b = function(callback){ | |
var date = new Date().getTime(); | |
for (var i=0;i<10000;i++) | |
callback(); | |
console.log("Completed a function test in", new Date().getTime()-date); | |
} | |
b(function(){ | |
var date = new Date().getTime(); | |
//console.log("Started A at:", date-globalTime); | |
for(var i=0;i<=12000;i++){ | |
var obj = Object.create({}); | |
delete obj; | |
} | |
//console.log("Done A", new Date().getTime()-date); | |
}); | |
b(function(){ | |
var date = new Date().getTime(); | |
//console.log("Started B at:", date-globalTime); | |
for (var i=0;i<12000;i++){ | |
var obj = Object.create({}); | |
delete obj; | |
} | |
//console.log("Done B", new Date().getTime()-date); | |
}); | |
console.log("Program End at", new Date().getTime()-globalTime); |
var globalTime = new Date().getTime(); | |
var a = function(){ | |
var date = new Date().getTime(); | |
//console.log("Started A at:", date-globalTime); | |
for(var i=0;i<=12000;i++){ | |
var obj = Object.create({}); | |
delete obj; | |
} | |
//console.log("Done A", new Date().getTime()-date); | |
} | |
var c = function(){ | |
var date = new Date().getTime(); | |
//console.log("Started B at:", date-globalTime); | |
for (var i=0;i<12000;i++){ | |
var obj = Object.create({}); | |
delete obj; | |
} | |
//console.log("Done B", new Date().getTime()-date); | |
} | |
var b = function(callback){ | |
var date = new Date().getTime(); | |
for (var i=0;i<10000;i++) | |
callback(); | |
console.log("Completed a Function Test in", new Date().getTime()-date); | |
} | |
console.log("Defined Everything in", new Date().getTime()-globalTime); | |
b(a); | |
b(c); | |
console.log("Program End at", new Date().getTime()-globalTime); |
In both the cases i.e. named and anonymous the architecture of garbage collection remains the same, the Object Counter running in both the environment is same, its the architecture that lacks sustainability and reliability.
Also with named memory points, I mean to say that whenever my script is initialised it should move all of my Anonymous as Well as Named Functions to a defined block of memory, or in layman term assign them some memory(just like C), so that the User Space, Virtual Space and Data Space becomes quite maintainable, but this can't be done as JS is a runtime compiled language so the architecture just didn't fit well in this.
Hope that clears what I am trying to say 😄
I will update with GC and other internal stuff when I get dTrace or SystemTap on my system, as of now quite busy for it.
Just for fun, I executed a same type of test for just anonymous function in php
<?php
$time = time();
class myClass{
public function __construct($callback){
$callback(1);
}
}
class testClass{
public function __contruct($value){
$value++;
}
}
$callback = function($i){
for ($i=0;$i<12000;$i++){
$object = new testClass($i);
}
};
for ($i=0;$i<10000;$i++)
$obj = new myClass($callback);
echo "It took :".time()-$time."ms";
and the results might mesmerise you:
It took :18ms
real 0m17.837s
user 0m17.762s
sys 0m0.044s
Nothing more to justify the amount of degradation of performance in JavaScript.
For an ideal NodeJs server running a decent service with a load of 100000 Users at a concurrency of 1000, the rate at which objects gets created is a function of e^x where x = number of Users at an instance on our node server. Now if you follow the V8 garbage collection(overview) https://developers.google.com/v8/design#garb_coll you can find out how it works and how it can impact your process.