Closed nkhil closed 3 years ago
We could use new Function()
, would you like to send a PR?
Thanks @mcollina , yes I'd love to create a PR! I'll come back to you shortly!
@mcollina I tried replacing eval()
with a regular function (example below) and ran a benchmark test after 50k iterations each. eval()
performed ~23% better.
const redactor = o => redact(o, JSON.stringify(paths))
return redactor
I don't know enough about eval()
or new Function()
currently to make that change. Based on my own benchmarking, I'm happy to go with eval()
and close this issue.
However, would you be so kind to describe how eval()
is able to be faster compared to a regular function? I'm very curious.
I recommended to use new Function()
instead of eval()
. It's safer and it perform the same.
@mcollina I've played around with this some more, and I'm still unsure on how to access functions that are in scope.
For eg:
const addOne = num => num + 1;
const fn = new Function(10, '(num) => addOne(num)')
fn()
^ This throws an error.
Since I'm unsure on how to proceed to create a PR, I'm going to close this issue.
I noticed that
eval
is used in pino-noirAfter reading around a bit, I wasn't sure how eval was being used here to precompile the redactor function, and how it was being more efficient/faster?
Also asking since I read this on MDN:
is there are any benchmarks on using eval vs not using it?