Skip to content

Commit 1cb437b

Browse files
authored
Merge pull request #95 from dikmax/faster-replicate
Faster replicate.
2 parents 43d3690 + 97af474 commit 1cb437b

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/Data/Array.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,17 @@ exports.range = function (start) {
1616
};
1717
};
1818

19-
exports.replicate = function (count) {
19+
var replicate = function (count) {
20+
return function (value) {
21+
if (count < 1) {
22+
return [];
23+
}
24+
var result = new Array(count);
25+
return result.fill(value);
26+
};
27+
};
28+
29+
var replicatePolyfill = function (count) {
2030
return function (value) {
2131
var result = [];
2232
var n = 0;
@@ -27,6 +37,11 @@ exports.replicate = function (count) {
2737
};
2838
};
2939

40+
// In browsers that have Array.prototype.fill we use it, as it's faster.
41+
exports.replicate = typeof Array.prototype.fill === "function" ?
42+
replicate :
43+
replicatePolyfill;
44+
3045
exports.fromFoldableImpl = (function () {
3146
// jshint maxparams: 2
3247
function Cons(head, tail) {

0 commit comments

Comments
 (0)