fast-xml-parser icon indicating copy to clipboard operation
fast-xml-parser copied to clipboard

Performance benefit in exports.buildOptions

Open sndjones007 opened this issue 6 years ago • 8 comments

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.

sndjones007 avatar May 19 '20 20:05 sndjones007

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.

github-actions[bot] avatar May 19 '20 20:05 github-actions[bot]

Thanks @sndjones007 Have you compared the performance after this change?

amitguptagwl avatar May 20 '20 01:05 amitguptagwl

I have compared the chunk of code in JSBench website. But not the whole code. It showed me 83% improved performance.

sndjones007 avatar May 20 '20 12:05 sndjones007

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.

amitguptagwl avatar May 21 '20 00:05 amitguptagwl

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.

sndjones007 avatar May 21 '20 06:05 sndjones007

I'm glad to hear that. Do the same thing in node.js without running on different browsers.

amitguptagwl avatar May 22 '20 00:05 amitguptagwl

image 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

sndjones007 avatar May 22 '20 07:05 sndjones007

done

amitguptagwl avatar Dec 09 '21 01:12 amitguptagwl