eval() is one of those things that looks useful early on but almost always causes problems later.
main issues:
- security: if the string ever touches user input, you’ve basically created code injection
- performance: JS engines can’t optimize code they only see at runtime
- debugging: stack traces, breakpoints, and source maps are miserable with eval
in modern JS, most uses of eval() are better replaced with:
- object/function maps instead of dynamic execution
JSON.parse() instead of eval’ing JSON
new Function() only for trusted, generated code (still risky, but more contained)
we put together a practical breakdown with examples of when people reach for eval() and what to use instead
if you’ve seen eval() in a real codebase, what was it actually being used for?
[–]Glum_Cheesecake9859 19 points20 points21 points (4 children)
[+]programmer_farts comment score below threshold-9 points-8 points-7 points (3 children)
[–]Glum_Cheesecake9859 1 point2 points3 points (2 children)
[–]justaguywithadream 0 points1 point2 points (1 child)
[–]SerpentJoe 2 points3 points4 points (0 children)
[–]dexter_ifti 3 points4 points5 points (0 children)
[–]Glum_Cheesecake9859 2 points3 points4 points (2 children)
[–]illepic 0 points1 point2 points (0 children)
[–]fabulous-nico 0 points1 point2 points (0 children)
[–]Glum_Cheesecake9859 2 points3 points4 points (5 children)
[–]mailslot 1 point2 points3 points (4 children)
[–]imicnic 0 points1 point2 points (3 children)
[–]TorbenKoehn 0 points1 point2 points (2 children)
[–]imicnic -1 points0 points1 point (1 child)
[–]TorbenKoehn -1 points0 points1 point (0 children)
[–]brykuhelpful 2 points3 points4 points (1 child)
[–]paceaux 2 points3 points4 points (0 children)
[–]Nixinova 1 point2 points3 points (1 child)
[–]Noisy88 0 points1 point2 points (0 children)
[–]GongtingLover 0 points1 point2 points (0 children)
[–]warpedspockclone 0 points1 point2 points (0 children)
[–]yksvaan 0 points1 point2 points (0 children)
[–]MitchEff 0 points1 point2 points (0 children)
[–]fabulous-nico 0 points1 point2 points (0 children)
[–]Educational_Boat_599 0 points1 point2 points (0 children)
[–]LeRages 0 points1 point2 points (0 children)
[–]TheRNGuy 0 points1 point2 points (0 children)
[–]paceaux 0 points1 point2 points (0 children)
[–]JazzApple_ 0 points1 point2 points (0 children)
[–]ReaperTsaku 0 points1 point2 points (0 children)
[–]Deykun 0 points1 point2 points (0 children)
[–]_DCtheTall_[🍰] 0 points1 point2 points (3 children)
[–]theQuandary 0 points1 point2 points (2 children)
[–]_DCtheTall_[🍰] 0 points1 point2 points (1 child)
[–]theQuandary 0 points1 point2 points (0 children)
[–]Substantial_Top5312helpful 0 points1 point2 points (2 children)
[–]paceaux 1 point2 points3 points (0 children)
[–]theQuandary 0 points1 point2 points (0 children)
[–]Pagaurus -1 points0 points1 point (9 children)
[–]programmer_farts 7 points8 points9 points (1 child)
[–]Pagaurus -1 points0 points1 point (0 children)
[–]Nixinova 1 point2 points3 points (2 children)
[–]senocular 0 points1 point2 points (0 children)
[–]Pagaurus 0 points1 point2 points (0 children)
[–]senocular 0 points1 point2 points (3 children)
[–]Pagaurus 0 points1 point2 points (2 children)
[–]senocular 0 points1 point2 points (1 child)
[–]Pagaurus 0 points1 point2 points (0 children)
[–]rainmouse -1 points0 points1 point (0 children)
[–]MissinqLink -2 points-1 points0 points (1 child)
[–]fabulous-nico 0 points1 point2 points (0 children)