3 ms·
function define() {...} function require() {...} define("one", function() { return 1; }); define("two", function() { return require("one") + require("one"
by xg15 10y ago
function define() {...}
function require() {...}
define("one", function() { return 1; });
define("two", function() { return require("one") + require("one"); });
define("three", function() { return require("two") + require("one"); });
three = require("three");
--->
three = 3;
There is a certain irony that now it's possible to do optimisations like that in javascript - a dynamically typed language with almost no compile time guarantees.
Meanwhile java used to have easy static analysis as a design goal (and I think a lot of boilerplate is due to that goal) but the community relies so much on reflection, unsafe access, dynamic bytecode generation, bytecode parsing etc that such an optimisation would be almost impossible to get right.
- lurker456 10y agoit's possible to do such optimizations for a (safe) subset of javascript, such as these pure functions. Arguably java has a larger subset even today.
- xg15 10y agoI think the remarkable point in the above optimisation was that the non-pure functions define() and require() were also subject of optimisation even though the optimizer had no special knowledge about them. Using symbolic execution, the optimizer nevertheless was able to reason about them.