Skip to content

Optimizing Stats objects by creating Date objects lazily #137

@fabiospampinato

Description

@fabiospampinato

What is the problem this feature will solve?

Making fs.stat calls faster.

What is the feature you are proposing to solve the problem?

There seem to be 4 Date objects being created per Stats object:

https://github.com/nodejs/node/blob/7981e2ee642ae2b551201aa120560eb7d54fde03/lib/internal/fs/utils.js#L491-L494

I think those should be created lazily, and cached after they are read the first time for maximum backwards compatibility.

In a little local benchmark creating the equivalent number of Date objects in a loop seems responsible for roughly ~15% of the time that fs.statSync spends, and all those Date objects probably still left garbage to be collected behind that my little for loop wasn't accounting for.

I'd imagine it's pretty rare that somebody needs to read all 4 of those Date objects, and I actually never read any of those, so I'd expect this optimization to potentially translate to a ~15% speedup or near that for stat calls for many people.

What alternatives have you considered?

No reasonable alternatives, the only ones I can think of are using process['binding']('fs') directly, or switching to another runtime.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions