Hacker News new | ask | show | jobs
by junke 3412 days ago
Ideally, a template should be compiled into native code.
3 comments

Not necessarily.

1. that requires parsing the template at compile-time (making it non-overridable at runtime) or the ability to generate executable code at runtime which break in NX-mandatory environment (e.g. appstore applications)

2. the runtime version may be much faster than the compile-time one, e.g. the rust Regex crate has both a runtime Regex structure and a regex! compiler plugin, the latter is currently significantly slower at runtime: https://github.com/rust-lang/regex#usage-regex-compiler-plug...

Yes, "compile/load/execute" can take longer than simply "eval". You have to consider amortized costs: how often do you need to recompile your template (or your regex)? Likewise, are you really going to override the template at runtime or is the variable part of the template already compiled as branches in the resulting code? Whatever needs to be dynamic can be dynamic while compiling as much as possible ahead of time.
Elixir's EEx templating engine compiles templates into plain elixir functions, which makes the templates really fast.
There is a web framework (vibed.org) written in the D programming language that does compile templates to native code.