Skip to content

Use WeakMap to store/retrieve the format. #54

Open
@kricsleo

Description

@kricsleo

Describe the feature

Currently it uses Object.defineProperty to attach format info to the parsed object. How about using WeakMap to store/retrieve formats stored in a seperate global variable?

const formats = new WeakMap<any, FormatInfo>();

{
  // store 
  formats.set(obj, format)
}

{
  // retrieve
  formats.get(obj)
}

Benifits:

  • It avoids "polluting" the parsed object with an extra symbol preperty.
  • It's slightly faster:
benchmark                    avg (min … max) p75 / p99    (min … top 1%)
-------------------------------------------- -------------------------------
confbox.parseJSONC              7.91 µs/iter   7.58 µs  █                   
                       (7.42 µs … 963.21 µs)  11.63 µs  █                   
                     (592.00  b … 223.95 kb)   7.95 kb ▃█▃▂▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁▁

confbox.parseJSONCWithWeakMap   7.62 µs/iter   7.63 µs           █          
                         (7.60 µs … 7.65 µs)   7.64 µs ██  █     █   █      
                     (Infinity pb … -Infinity  b)       NaN ██▁██▁▁█▁██▁████▁▁▁▁█

summary
  confbox.parseJSONCWithWeakMap
   1.05x faster than confbox.parseJSONC

Additional information

  • Would you be willing to help implement this feature?

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions