Hello guys,

I've just stumbled upon an unbelievable JavaScript performance bug (or
feature, I can't decide).
I was trying to improve the performance of a Class constructor like this one:

---
CreateClass = function(parentClass, config) {
   //Code here to clean and validate arguments

   //Uncomment this to be 10x faster
   //var totallyInventedName = function() {
   //
   //}

   var classConstructor = function() {
      //A lot of stuff to copy 'config' to my prototype
   }

   //A lot of tweaks with classConstructor.prototype
   return classConstructor;
}

var Class = CreateClass(null, { /*methods here*/});
var instance = new Class();
---

Creating 100 instances takes about 300ms (FF 3.6, measured with
FireBug and internal timers using Date()). The weird thing is that if
I add a function definition before defining classConstructor
(commented in the example), the code only takes 30ms to run, and no
code is calling this new function (100% sure about this point). It
makes no sense, the new code should be slower because it's creating
100 new functions.

I've checked everything several times, restarted Firefox, started with
a new Firefox profile, even reinstalled Firefox, and the results are
consistent: 300ms without the function, 30ms whit it.

It makes completely no sense to me. Can someone please shed some light
into this?

--
S.Cinos
JavaScript Developer at Tuenti.com

_______________________________________________
JSMentors mailing list
[email protected]
http://jsmentors.com/mailman/listinfo/jsmentors_jsmentors.com

List Archive:
http://jsmentors.com/pipermail/jsmentors_jsmentors.com/

Reply via email to