Performance benefit in exports.buildOptions
https://github.com/NaturalIntelligence/fast-xml-parser/blob/6f59838b9c919df6a6a83fa8d4b4cc20d25a0d73/src/util.js#L69
Rather than loop through the options object to copy its properties and create a newOptions object. There is a much better way of creating a new options object using Object.assign() function.
You can simply do a
Object.assign(defaultOptions, options);
(You have to do it at every place where this call to buildOptions is made)
The above code is much more clean and improves the performance.
I'm glad you find this repository helpful. I'll try to address your issue ASAP. You can watch the repo for new changes or star it.
Thanks @sndjones007 Have you compared the performance after this change?
I have compared the chunk of code in JSBench website. But not the whole code. It showed me 83% improved performance.
I'll try it in the next release. But as I remember, I have already tried this option around 1.5 years back and found no improvement.
I again did some investigation in Chrome Version 81.0.4044.138 (Official Build) (64-bit) and Firefox 76.0.1 (64 bit). I used JSBench.me and jsben.ch. Below are the two code samples I used to test:
Code with Object.assign
var obj1 = {
item1: 23,
item2: 'item2',
item3: () => 'item3',
item4: 89,
item5: 234.45
};
var obj2 = {
item3: 'item3',
item4: 'item4',
item7: 23.23,
item8: 'item8'
};var ob3 = Object.assign(obj2, obj1);
Code with for loop
var obj1 = {
item1: 23,
item2: 'item2',
item3: () => 'item3',
item4: 89,
item5: 234.45
};
var obj2 = {
item3: 'item3',
item4: 'item4',
item7: 23.23,
item8: 'item8'
};var ob4 = {};
for (let i = 0; i < props.length; i++) {
if (obj2[props[i]] !== undefined) {
ob4[props[i]] = obj2[props[i]];
} else {
ob4[props[i]] = obj1[props[i]];
}
}
In Firefox, the difference is not that much almost 10%, but in Chrome its significant (70%). Also, by using Object.assign the code is also minimized and concise. But, at the end test at your end and make your own judgement. Actually I am a student and doing a research of Parsers at my past time.
I'm glad to hear that. Do the same thing in node.js without running on different browsers.
The above is tested in pure node js APIs.
node version v12.16.3
npm version 6.14.4
index.txt Attached file is used to test (change extension to .js)
However, please note you have to handle exception as mentioned in Mozilla Developer Site
done