chakra-core / chakra-core/ChakraCore

Inlining seems to lack sufficient sophistication for non-trivial functional composition use cases

Open
#2,866 6 comments 0 reactions 1 assignee Assigned to @LouisLaf View on GitHub
Performance
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:

![image](https://cloud.githubusercontent.com/assets/298883/25307905/02732fee-27ed-11e7-957c-fc1e31e1588e.png)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.