chakra-core / chakra-core/ChakraCore
Inlining seems to lack sufficient sophistication for non-trivial functional composition use cases
- Dominant language
- JavaScript
- Stars
- 9.3k
- Forks
- 1.2k
- PR merge metrics
- No merged PRs in 30d
Description
Consider the following code:
```js
function update(isAdd, value, then) {
let fn = isAdd
? (x) => x + value
: (x) => x - value;
return then(fn);
}
function multiply(value) {
return fn => x => fn(x) * value;
}
function divide(value) {
return fn => x => fn(x) / value;
}
const fn1 = update(true, 10, divide(5));
const fn2 = update(false, 13, multiply(2));
function run(x) {
return fn1(x) + fn2(x);
}
```
The function `run()` is obviously compile-time inlineable, because all function calls can be cleanly flattened. V8 in Chrome 58 flattens the whole chain perfectly, yielding identical performance between:
```js
target = run(target);
```
and the equivalent manually-flattened version:
```js
target = (target + 10)/5 + (target - 13)*2;
```
Here is the test in jsperf, ready to run: https://jsperf.com/functional-composition-with-inlining
Firefox shows roughly a 10% performance drop for the functional version, as compared to the baseline. Safari sits somewhere between Chrome and Firefox.
Edge performance on the same test is shockingly bad:

Contributor guide
Assessment
This issue has not been assessed yet.