eemeli / yaml

YAML parser and stringifier for JavaScript
https://eemeli.org/yaml
ISC License
1.31k stars 115 forks source link

Performance issue on relatively small file #537

Open Porges opened 7 months ago

Porges commented 7 months ago

Describe the bug

Parsing the file located here with yaml.parse(str, {merge:true}) takes a very long time. Could there be something non-linear in the length of the file?

Currently it's under 500k:

$ wc bibliography.yaml
 18610  48360 489877 bibliography.yaml

To Reproduce Steps to reproduce the behaviour.

const yaml = require('yaml');
const fs = require('fs');

fs.readFile("bibliography.yaml", 'utf8', (_, data) => {
    console.time('yaml-parse');
    const loaded = yaml.parse(data, {merge: true});
    console.timeEnd('yaml-parse');
});

Output:

yaml-parse: 10.501s

Expected behaviour A clear and concise description of what you expected to happen.

By comparison, doing the same thing with js-yaml takes only:

const yaml = require('js-yaml');
const fs = require('fs');

fs.readFile("bibliography.yaml", 'utf8', (_, data) => {
    console.time('yaml-load');
    const loaded = yaml.load(data);
    console.timeEnd('yaml-load');
});

Output:

yaml-load: 125.814ms

Versions (please complete the following information):

Additional context Add any other context about the problem here.

eemeli commented 7 months ago

Interesting. Thank you for sharing the file that's demonstrating this! Looks like the slowdown is during the document -> JS conversion. Probably due to the alias resolution?

I'll see when I can find time to dig into this properly; might take a while.