From f727c5b90d4177c7999d93458c93796f985eaccd Mon Sep 17 00:00:00 2001 From: Dmytro Brahinets Date: Thu, 26 Jun 2025 19:59:49 +0300 Subject: [PATCH] Update 4-improved.js Provided an alternative point of view regarding the V8 optimisations. --- JavaScript/4-improved.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/JavaScript/4-improved.js b/JavaScript/4-improved.js index 20a016e..aa45b0f 100644 --- a/JavaScript/4-improved.js +++ b/JavaScript/4-improved.js @@ -1,7 +1,13 @@ 'use strict'; const poolify = (factory, { size, max }) => { - const instances = new Array(size).fill(null).map(factory); + // It seems that preallocated array with defined size + // and using c-like for-loop will be more efficient in terms of V8 optimisation than + // const instances = new Array(size).fill(null).map(factory); + const instances = new Array(size); + for(let i = 0; i < size; i++){ + instances[i] = factory(); + } const acquire = () => instances.pop() || factory();