Repository URL to install this package:
|
Version:
6.0.4 ▾
|
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var LARGE_ARRAY_SIZE = 200;
var is_1 = require("../is");
var size_1 = require("../util/size");
var numberFromZero_1 = require("../util/numberFromZero");
// const lengthMinusOne = require('../util/lengthMinusOne')
// const lengthFrom0 = require('../util/lengthFromZero')
// @TODO also ensure we coerce this number...?
// @NOTE calls from0 twice but inlined makes less diff than adding pointer
var arrFrom0 = function (x) { return new Array(numberFromZero_1.default(x) > LARGE_ARRAY_SIZE ? 0 : numberFromZero_1.default(x)); };
/**
* @desc make a new empty Array filled with a pre-allocated-length-from-zero
* @memberOf array
* @name preAllocate
* @since 5.0.0
* @func
*
* @param {Object|Array|number} x array or object to return an empty array.of.fill (pre-allocated)
* @return {Array} preallocated array full of undefined
*
* @TODO not sure about pre-allocating objects?
*
* {@link https://github.com/facebook/react/blob/8f4d30737def9fa3456149826414643b5cbbe4bf/docs/docs/optimizing-performance.md react-opt}
* {@link https://thewayofcode.wordpress.com/tag/array-pre-allocation/ the-way-of-code-array}
* {@link https://www.html5rocks.com/en/tutorials/speed/v8/#toc-topic-numbers html-5-rocks-v8}
* @see {@link html5-rocks-v8}
* @see {@link the-way-of-code-array}
* @see {@link react-opt}
* @see is/numberPrimitive
* @see is/array
* @see util/size
*
* @NOTE could be an `||` but it's annoying how it deopts sometimes (arr checks)
*
* @example
*
* preAllocate({eh: true})
* //=> {}
*
* preAllocate([1, 2, 10])
* //=> [undefined, undefined, undefined]
*
* preAllocate(2)
* //=> [undefined, undefined]
*
*/
function preAllocate(x) {
// @TODO now that size is better, this can just be...
// return arrFrom0(size(x))
return is_1.isNumberPrimitive(x)
? arrFrom0(x)
: is_1.isArray(x)
? arrFrom0(x.length)
: arrFrom0(size_1.default(x));
}
exports.default = preAllocate;
//# sourceMappingURL=preAllocate.js.map