xmlbuilder2
xmlbuilder2 copied to clipboard
Performance regression in xmlbuilder2
Describe the bug
After some performance profiling in our application it appears that xmlbuilder2 is approximately 2-3 times slower than xmlbuilder for our use case (XML sitemaps). Can something be done to get the performance more in-line with the original library?
To Reproduce I'm attaching two test scripts below. When I run them I get these results:
✗ node xmlbuilder.js
Execution time: 3794ms
✗ node xmlbuilder2.js
Execution time: 10706ms
xmlbuilder.js
const builder = require("xmlbuilder");
const articles = [];
for (let i = 0; i < 500000; i += 1) {
articles.push({
loc: 'https://example.org',
changefreq: 'weekly',
priority: 0.6,
});
}
const start = new Date();
const obj = {
urlset: {
'@xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'@xmlns': 'http://www.sitemaps.org/schemas/sitemap/0.9',
'@xsi:schemaLocation':
'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd',
url: [...articles],
},
};
const feed = builder.create(obj, { encoding: 'utf-8' });
const output = feed.end({ pretty: false });
const end = new Date() - start;
console.info('Execution time: %dms', end);
xmlbuilder2.js
const { create } = require('xmlbuilder2');
const articles = [];
for (let i = 0; i < 500000; i += 1) {
articles.push({
loc: 'https://example.org',
changefreq: 'weekly',
priority: 0.6,
});
}
const start = new Date();
const obj = {
urlset: {
'@xmlns:xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'@xmlns': 'http://www.sitemaps.org/schemas/sitemap/0.9',
'@xsi:schemaLocation':
'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd',
url: [...articles],
},
};
const doc = create(obj).dec({ encoding: 'UTF-8' });
const xml = doc.end({ prettyPrint: false });
const end = new Date() - start;
console.info('Execution time: %dms', end);
Expected behavior
Similar performance as the xmlbuilder library.
Version:
- node.js:
v14.15.1 - xmlbuilder2
2.4.0 - xmlbuilder
15.1.1