Skip to content

Instantly share code, notes, and snippets.

@DronRathore
Created September 18, 2014 14:31
Show Gist options
  • Save DronRathore/88805853b40d7001b651 to your computer and use it in GitHub Desktop.
Save DronRathore/88805853b40d7001b651 to your computer and use it in GitHub Desktop.
Node Anonymous Function Vs Named Functions
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);
@DronRathore
Copy link
Author

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 😄

@DronRathore
Copy link
Author

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.

@DronRathore
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment