From 1a32b3b244bbe2da0527a3e6f717f961e53f5cc4 Mon Sep 17 00:00:00 2001 From: Vladislav Zarakovsky Date: Sun, 21 Jun 2015 17:06:13 +0300 Subject: [PATCH] Create for.js Same as https://github.com/jonschlinkert/array-unique/blob/master/benchmark/code/while.js, but preserves the order of elements in result array, which is nice to have. Performance is almost as good as while.js BTW, is there any reason the implementation in https://github.com/jonschlinkert/array-unique/blob/master/index.js is not from https://github.com/jonschlinkert/array-unique/blob/master/benchmark/code/while.js ? For me on node 0.12.0 array-uniq with Set is the fastest on arrays bigger than 1000 elements. In all other cases https://github.com/jonschlinkert/array-unique/blob/master/benchmark/code/while.js is the fastest. --- benchmark/code/for.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 benchmark/code/for.js diff --git a/benchmark/code/for.js b/benchmark/code/for.js new file mode 100644 index 0000000..ba5cf02 --- /dev/null +++ b/benchmark/code/for.js @@ -0,0 +1,14 @@ +'use strict'; + +module.exports = function(arr) { + var len = arr.length; + var res = []; + + for (var i = 0; i < len; i++) { + var curr = arr[i]; + if (res.indexOf(curr) === -1) { + res.push(curr); + } + } + return res; +};