UNPKG

scrawl-canvas

Version:

Responsive, interactive and more accessible HTML5 canvas elements. Scrawl-canvas is a JavaScript library designed to make using the HTML5 canvas element easier, and more fun

60 lines (37 loc) 1.26 kB
// # Array pools // Used internally to minimise the number of Array objects created/disposed during operations // #### Imports import { constructors } from '../core/library.js'; // Shared constants import { _create, _setPrototypeOf } from './shared-vars.js'; // Local constants const T_GENERIC_ARRAY = 'GenericArray'; // #### GenericArray constructor const GenericArray = function () { const a = []; _setPrototypeOf(a, GenericArray.prototype); return a; }; constructors.GenericArray = GenericArray; // #### GenericArray prototype const P = GenericArray.prototype = _create(Array.prototype); P.constructor = GenericArray; P.type = T_GENERIC_ARRAY; // #### GenericArray pool const genericArrayPool = []; export const requestArray = function () { if (!genericArrayPool.length) genericArrayPool.push(new GenericArray()); return genericArrayPool.shift(); }; export const releaseArray = function (...args) { args.forEach(a => { if (a && a.type === T_GENERIC_ARRAY) { a.length = 0; genericArrayPool.push(a); if (genericArrayPool.length > 20) { console.log('purging genericArrayPool'); genericArrayPool.length = 10; } } }); };