tdewolff / parse

Go parsers for web formats
MIT License
408 stars 64 forks source link

JS: add ast JSWriteTo method #104

Closed ttttmr closed 1 year ago

ttttmr commented 1 year ago

I often edit ast, modify some data, and then convert ast back to js code, ast.JS() is too slow and takes up a lot of memory

Using buffers can greatly improve efficiency

BenchmarkJsAstJSWriteTo-2        10           194873291 ns/op      36253063 B/op    1031732 allocs/op
BenchmarkJsAstJS-2               10          1559868949 ns/op    2160659760 B/op    1057158 allocs/op
// JSWriteTo
ast, _ := js.Parse(parse.NewInputBytes(body), js.Options{})
...
var buf bytes.Buffer
ast.JSWriteTo(&buf)

// JS
ast, _ := js.Parse(parse.NewInputBytes(body), js.Options{})
...
ast.JS()

If this is useful, consider this pr

ttttmr commented 1 year ago

The implementation of some JS() methods may need to wait for #103 to be resolved

tdewolff commented 1 year ago

Excellent addition, I've incorporated the code, thanks!