Код: Выделить всё
// object with persistent shape
class Obj {
prop = 42
}
// function always called with persistent types/shapes of args
function obj_accessor(obj) {
return obj.prop;
}
function main() {
const obj = new Obj();
// this call is highly likely to be inlined
const val = obj_accessor(obj);
console.log(val);
}
< /code>
Но если я использую функции более высокого порядка, в каких случаях он будет выполнять внедрение, если в таком?class Foo {
bar = 42;
}
class Bar {
foo = 42;
}
function foo_accessor(obj) {
return obj.bar;
}
function bar_accessor(obj) {
return obj.foo;
}
function main(obj, accessor) {
// will inlining be performed at all ?
// is it per call site or per function
// so after passing more then single accessor it
// can not be optimized any more ?
const val = accessor(obj);
console.log(val)
}
// accessor not statically know -> no inlining ?
main(...(Math.random() < 0.5 ? [new Foo(), foo_accessor] : [new Bar(), bar_accessor]));
// accessor statically know -> it can be inlined
main(new Foo(), foo_accessor);
// accessor statically know -> it can be inlined
main(new Bar(), bar_accessor);
Подробнее здесь: https://stackoverflow.com/questions/796 ... n-inlining