-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
XML Writer Performance Consideration #1
Comments
The issue is discussed at StackOverflow: Advantages of using prototype, vs defining methods straight in the constructor? The fast quote: |
I can not remember why I used definning functions. For my others packages, I use prototype... |
I got a performance testing with your current module and slightly optimized module. I moved to the prototype the single method startDocument. The test code is there
Results are very surprising, I expected performance gain but not so big. zemlyanov@linux-sgvr:~/tmp> node test.js zemlyanov@linux-sgvr:~/tmp> node test.js After: zemlyanov@linux-sgvr:~/tmp> node test.js zemlyanov@linux-sgvr:~/tmp> node test.js I got another test. First I made a bare constructor function without "child" methods inside. The 1,000,000 objects were created in 20ms. Then I added a single method this.method = function() ... and next run taken some 210ms to run. Resume: performance gain of moving methods to prototype is very significant, it's worth job to do. I will make pull request. I will try to minimize code impact by using literal object style for defining prototype. |
I like your xml-writer, in npm you can see another dependancy. I use your module to generate dozens of small XMLs in a second. I construct lots of XMLWriter objects.
Each time XML writer is created, constructor init all the methods. We can remove initing methods in constructor by using XmlWriter.prototype. Prototype is fully initialized in the first require, while constructor is called million times a day. I know it breaks the clouse, but I think it should not be a problem as you use this.something and this is properly inited in prototype methods
I propose changing
this.startElement = function(name) {
...
with
XMLWriter.prototype.startElement = function(name) {
...
So instead of the single constructor function with all the job there we will have a lightweight constructor plus a prototype that is shared among millions of XMLWriter instances
The text was updated successfully, but these errors were encountered: