From 87ec5f42c24326d039726b9b24278deef0b25444 Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Mon, 23 Nov 2015 03:37:00 +0000 Subject: [PATCH 1/5] Faster ascending array index --- lib/events.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/events.js b/lib/events.js index 589dc4e79ea234..167f8ed951457c 100644 --- a/lib/events.js +++ b/lib/events.js @@ -426,9 +426,9 @@ function spliceOne(list, index) { list.pop(); } -function arrayClone(arr, i) { - var copy = new Array(i); - while (i--) +function arrayClone(arr, length) { + var copy = new Array(length); + for (var i = 0; i < length; i++) copy[i] = arr[i]; return copy; } From 8f39c9e036c704c7d3b2a2ff3b8440c965956361 Mon Sep 17 00:00:00 2001 From: alejandro Date: Tue, 24 Nov 2015 11:09:35 +0000 Subject: [PATCH 2/5] New revision of arrayClone in events.js using for or slice depending on the length of the array --- lib/events.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/events.js b/lib/events.js index 167f8ed951457c..4e67ecd056189e 100644 --- a/lib/events.js +++ b/lib/events.js @@ -427,8 +427,13 @@ function spliceOne(list, index) { } function arrayClone(arr, length) { - var copy = new Array(length); - for (var i = 0; i < length; i++) - copy[i] = arr[i]; - return copy; -} + if (length === 1) { + return [arr[0]]; + } else if (length < 50) { + var copy = new Array(length); + for (var i = 0; i < length; i++) + copy[i] = arr[i]; + return copy; + } + return arr.slice(); +} \ No newline at end of file From 66dd7c72f0b2e61de6ab719d98c4d41dbe810a58 Mon Sep 17 00:00:00 2001 From: alejandro Date: Tue, 24 Nov 2015 12:25:56 +0000 Subject: [PATCH 3/5] Added comment to new version of arrayClone in events.js --- lib/events.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/events.js b/lib/events.js index 4e67ecd056189e..9e6c47f51007e1 100644 --- a/lib/events.js +++ b/lib/events.js @@ -426,6 +426,8 @@ function spliceOne(list, index) { list.pop(); } +// Optimized common usages (listeners < 50) and using slice for +// large arrays so it scales much better function arrayClone(arr, length) { if (length === 1) { return [arr[0]]; From 3b899ed9f054f84280afac53b10e57570ac4f820 Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Mon, 30 Nov 2015 21:12:40 +0000 Subject: [PATCH 4/5] Added Comment to for loop and else if was replaced by if so the code looks cleaner --- lib/events.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/events.js b/lib/events.js index 9e6c47f51007e1..1a063f0ac94793 100644 --- a/lib/events.js +++ b/lib/events.js @@ -431,11 +431,16 @@ function spliceOne(list, index) { function arrayClone(arr, length) { if (length === 1) { return [arr[0]]; - } else if (length < 50) { + } + + // For loop is faster than Array#slice() with arrays + // smaller than 50 + if (length < 50) { var copy = new Array(length); for (var i = 0; i < length; i++) copy[i] = arr[i]; return copy; } + return arr.slice(); } \ No newline at end of file From 17677e0b2bcc1e530996804c327a32e173cf556d Mon Sep 17 00:00:00 2001 From: Alejandro Santiago Date: Mon, 18 Jan 2016 21:01:18 +0000 Subject: [PATCH 5/5] After a discussion, 27 seems a better choice than 50 as the magic number. Improved comments and added missing new line at the end of file. --- lib/events.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/events.js b/lib/events.js index 1a063f0ac94793..67e224af370e2f 100644 --- a/lib/events.js +++ b/lib/events.js @@ -426,16 +426,16 @@ function spliceOne(list, index) { list.pop(); } -// Optimized common usages (listeners < 50) and using slice for -// large arrays so it scales much better +// As of node.js v5.4.1 (v8 v4.6.85.31) benchmarks showed, a simple for +// loop performs better than Array#slice() for arrays smaller than 27 +// elements, it is worth it, because it's more likely to use EventEmitters +// with a small number of listeners. function arrayClone(arr, length) { if (length === 1) { return [arr[0]]; } - // For loop is faster than Array#slice() with arrays - // smaller than 50 - if (length < 50) { + if (length < 27) { var copy = new Array(length); for (var i = 0; i < length; i++) copy[i] = arr[i]; @@ -443,4 +443,4 @@ function arrayClone(arr, length) { } return arr.slice(); -} \ No newline at end of file +}