confbox icon indicating copy to clipboard operation
confbox copied to clipboard

Use `WeakMap` to store/retrieve the format.

Open kricsleo opened this issue 10 months ago • 0 comments

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

  • [x] Would you be willing to help implement this feature?

kricsleo avatar Mar 14 '25 03:03 kricsleo