I'm trying to understand why arrow functions might run faster or slower in different javascript contexts.
Given the following three options:
- JS Statement
- (e.g.
paragraph.textContent = 'Loop iteration ' + (i + 1)
)
- JS Anonymous Arrow Function
- (e.g.
(i) => paragraph.textContent = 'Loop iteration ' + (i + 1)
)
- JS Arrow Function assigned to Named Variable
- (e.g.
myFunction(i)
)
I would have guessed (entirely wrongly) that the raw JS Statement might run the fastest, followed by the Arrow Function assigned to Named Variable, and that the Anonymous Arrow Function might run the slowest.
So I wrote a quick to test to verify that my assumptions were correct:
let paragraph = document.querySelector('p');
let button = document.querySelector('button');
let rawStatement = document.querySelector('.rawStatement');
let anonymousFunction = document.querySelector('.anonymousFunction');
let functionAssignedToVariable = document.querySelector('.functionAssignedToVariable');
const runRawStatement = () => {
let scriptDurations = [];
for (let h = 0; h < 100; h++) {
let scriptStart = window.performance.now();
for (let i = 0; i < 10000; i++) {
paragraph.textContent = 'Loop iteration ' + (i + 1);
}
let scriptEnd = window.performance.now();
scriptDurations.push((scriptEnd - scriptStart));
}
return ((scriptDurations.reduce((a, b) => a + b, 0)) / 100);
}
const runAnonymousFunction = () => {
let scriptDurations = [];
for (let h = 0; h < 10; h++) {
let scriptStart = window.performance.now();
for (let i = 0; i < 10000; i++) {
((i) => paragraph.textContent = 'Loop iteration ' + (i + 1))();
}
let scriptEnd = window.performance.now();
scriptDurations.push((scriptEnd - scriptStart));
}
return ((scriptDurations.reduce((a, b) => a + b, 0)) / 10);
}
const runFunctionAssignedToVariable = () => {
let scriptDurations = [];
for (let h = 0; h < 10; h++) {
const myFunction = (i) => paragraph.textContent = 'Loop iteration ' + (i + 1);
let scriptStart = window.performance.now();
for (let i = 0; i < 10000; i++) {
myFunction(i);
}
let scriptEnd = window.performance.now();
scriptDurations.push((scriptEnd - scriptStart));
}
return ((scriptDurations.reduce((a, b) => a + b, 0)) / 10);
}
const runTimers = () => {
let runRawStatementDuration = runRawStatement();
document.querySelector('.rawStatement').textContent = runRawStatementDuration + 'ms';
let runRawFunctionDuration = runAnonymousFunction();
document.querySelector('.anonymousFunction').textContent = runRawFunctionDuration + 'ms';
let runVariableFunctionDuration = runFunctionAssignedToVariable();
document.querySelector('.functionAssignedToVariable').textContent = runVariableFunctionDuration + 'ms';
button.textContent = 'Re-run Script';
}
button.addEventListener('click', runTimers, false);
button {
display: inline-block;
cursor: pointer;
}
table {
margin: 12px 0;
border-collapse: collapse;
}
th,
td {
padding: 6px;
border: 1px solid rgb(0, 0, 0);
}
button {
display: inline-block;
margin-right: 6px;
}
<table>
<thead><th>Script Type</th><th>Average of 1000 run-throughs</th></thead>
<tr><td>JS Statement</td><td class="rawStatement"></td></tr>
<tr><td>Anonymous Arrow Function</td><td class="anonymousFunction"></td></tr>
<tr><td>Arrow Function assigned to Named Variable</td><td class="functionAssignedToVariable"></td></tr>
</table>
<button type="button">Run Script</button>
<em>(N.B. script takes about 5 seconds to run)</em>
<p></p>
However, consistently, the anonymous function is the fastest - by a degree of magnitude - followed (a long way behind) by the same anonymous function assigned to a variable and the slowest code to run is usually the raw JS statement.
Can anyone explain why the results consistently show that the anonymous function runs so much more slowly when assigned to a variable and why both the functions are faster than the raw JS statement.