Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use WeakMap to store/retrieve the format. #54

Open
1 task done
kricsleo opened this issue Mar 14, 2025 · 0 comments
Open
1 task done

Use WeakMap to store/retrieve the format. #54

kricsleo opened this issue Mar 14, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@kricsleo
Copy link
Member

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?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant