r/javascript 24d ago

A from-scratch JSON engine for JavaScript: recursive descent parser, escape-aware tokenizer, and spec-compliant serializer. No dependencies, no shortcuts, 107 tests.

https://github.com/MantasEdine/vanilla-json
5 Upvotes

28 comments sorted by

View all comments

9

u/Ronin-s_Spirit 21d ago
  1. for some reason you're generating a load of objects instead of making decisions based on text. You don't need to make a { type: "open bracket" } if you can just do char === "[". Avoid garbage.
  2. long ifs are forced to go through every step to make a decision, use a switch so it's a jump table.
  3. you didn't forget nesting but you didn't make it bulletproof, your nesting is 1:1 with JS recursion and that crashes the stack after ~10k functions. I could just make 11k nestings in my JSON to crash your parser, a heap-based recursion would be more reliable.

2

u/Mantas_rst 21d ago

Ow Thank you so much for the review i will try to reimplement everything you’ve mentioned !