From 973b7cb2d2ccb2d4920df0477e2707657c70dd6d Mon Sep 17 00:00:00 2001 From: Walik23 Date: Thu, 12 Jun 2025 13:14:57 +0300 Subject: [PATCH 1/6] task1: optimize 4-bad.js --- JavaScript/4-bad.js | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/JavaScript/4-bad.js b/JavaScript/4-bad.js index a706074..f81c13a 100644 --- a/JavaScript/4-bad.js +++ b/JavaScript/4-bad.js @@ -1,32 +1,29 @@ 'use strict'; -// TODO: Refactor to respect SoC principle and -// optimize JavaScript for V8 - -const poolify = (factory, options, size, max) => { - const instances = []; // Preallocate array - for (let i = 0; i < size; i++) { // Use Array methods instead of loop - const instance = factory(...options); // Avoid array destructuring - instances.push(instance); - } - - return (instance) => { // Respect SoC and SOLID/SRP - if (instance) { // Avoid if-statement - if (instances.length < max) { - instances.push(instance); - } +const poolify = (factory, size, max) => { + const instances = new Array(size).fill(null).map(factory); + + const acquire = () => instances.pop() || factory(); + + const release = (instance) => { + if (instances.length < max) { + instances.push(instance); } - instance = instances.pop(); // Do not reassign incoming parameters - if (!instance) instance = factory(...options); - return instance; }; + + return { acquire, release }; }; // Usage const createBuffer = (size) => new Uint8Array(size); -const pool = poolify(createBuffer, [4096], 10, 15); -const instance = pool(); +const bufferSize = 4096; +const createFileBuffer = () => createBuffer(bufferSize); + +const size = 10; +const max = 15; +const pool = poolify(createFileBuffer, size, max); +const instance = pool.acquire(); console.log({ instance }); -pool(instance); +pool.release(instance); From de511cac0ad095fe33464f928f372f31a4c2f973 Mon Sep 17 00:00:00 2001 From: Walik23 Date: Thu, 12 Jun 2025 13:33:04 +0300 Subject: [PATCH 2/6] task2: Rewrite 4-improved.js to class syntax --- JavaScript/4-improved.js | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/JavaScript/4-improved.js b/JavaScript/4-improved.js index 20a016e..98ecedd 100644 --- a/JavaScript/4-improved.js +++ b/JavaScript/4-improved.js @@ -1,17 +1,21 @@ 'use strict'; -const poolify = (factory, { size, max }) => { - const instances = new Array(size).fill(null).map(factory); +class ObjectPool { + constructor (factory, { size, max }) { + this.factory = factory; + this.max = max; + this.instances = new Array(size).fill(null).map(() => this.factory()); + }; - const acquire = () => instances.pop() || factory(); + acquire () { + return this.instances.pop() || this.factory(); + }; - const release = (instance) => { - if (instances.length < max) { - instances.push(instance); + release (instance) { + if (this.instances.length < this.max) { + this.instances.push(instance); } }; - - return { acquire, release }; }; // Usage @@ -21,7 +25,7 @@ const createBuffer = (size) => new Uint8Array(size); const FILE_BUFFER_SIZE = 4096; const createFileBuffer = () => createBuffer(FILE_BUFFER_SIZE); -const pool = poolify(createFileBuffer, { size: 10, max: 15 }); +const pool = new ObjectPool(createFileBuffer, { size: 10, max: 15 }); const instance = pool.acquire(); console.log({ instance }); pool.release(instance); From 33bfda72713c52a47ce417cb15372e1f6df2cf22 Mon Sep 17 00:00:00 2001 From: Walik23 Date: Thu, 12 Jun 2025 14:04:14 +0300 Subject: [PATCH 3/6] task3: Implement async instance acquisition in closure version with queue and callbacks --- JavaScript/4-bad.js | 62 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 6 deletions(-) diff --git a/JavaScript/4-bad.js b/JavaScript/4-bad.js index f81c13a..c602d68 100644 --- a/JavaScript/4-bad.js +++ b/JavaScript/4-bad.js @@ -2,12 +2,33 @@ const poolify = (factory, size, max) => { const instances = new Array(size).fill(null).map(factory); + const queue = []; + let available = instances.length; - const acquire = () => instances.pop() || factory(); + const processQueue = () => { + while (queue.length > 0 && available > 0){ + const { callback } = queue.shift(); + callback(instances.pop()); + available--; + } + }; + + const acquire = (callback) => { + if(available > 0){ + const instance = instances.pop(); + available--; + callback(instance); + } + else { + queue.push({ callback }); + } + }; const release = (instance) => { if (instances.length < max) { instances.push(instance); + available++; + processQueue(); } }; @@ -21,9 +42,38 @@ const createBuffer = (size) => new Uint8Array(size); const bufferSize = 4096; const createFileBuffer = () => createBuffer(bufferSize); -const size = 10; -const max = 15; +const size = 2; +const max = 4; const pool = poolify(createFileBuffer, size, max); -const instance = pool.acquire(); -console.log({ instance }); -pool.release(instance); + +pool.acquire((instance1) => { + console.log('Acquired instance 1', instance1); + setTimeout(() => { + console.log('Releasing instance 1'); + pool.release(instance1); + }, 1000); +}); + +pool.acquire((instance2) => { + console.log('Acquired instance 2', instance2); + setTimeout(() => { + console.log('Releasing instance 2'); + pool.release(instance2); + }, 1500); +}); + +pool.acquire((instance3) => { + console.log('Acquired instance 3', instance3); + setTimeout(() => { + console.log('Releasing instance 3'); + pool.release(instance3); + }, 1000); +}); + +pool.acquire((instance4) => { + console.log('Acquired instance 4', instance4); + setTimeout(() => { + console.log('Releasing instance 4'); + pool.release(instance4); + }, 1500); +}); From 89a25a54be438737bdeae9df4b2fd6ae0f988e70 Mon Sep 17 00:00:00 2001 From: Walik23 Date: Thu, 12 Jun 2025 14:37:08 +0300 Subject: [PATCH 4/6] task4: Implement async instance acquisition in classes version with queue and callbacks --- JavaScript/4-improved.js | 60 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/JavaScript/4-improved.js b/JavaScript/4-improved.js index 98ecedd..9a32aec 100644 --- a/JavaScript/4-improved.js +++ b/JavaScript/4-improved.js @@ -5,15 +5,34 @@ class ObjectPool { this.factory = factory; this.max = max; this.instances = new Array(size).fill(null).map(() => this.factory()); + this.queue = []; + this.available = this.instances.length; }; - acquire () { - return this.instances.pop() || this.factory(); + _processQueue() { + while (this.queue.length > 0 && this.available > 0){ + const { callback } = this.queue.shift(); + callback(this.instances.pop()); + this.available--; + } + }; + + acquire (callback) { + if(this.available > 0){ + const instance = this.instances.pop(); + this.available--; + callback(instance); + } + else{ + this.queue.push({ callback }); + } }; release (instance) { if (this.instances.length < this.max) { this.instances.push(instance); + this.available++; + this._processQueue(); } }; }; @@ -25,7 +44,36 @@ const createBuffer = (size) => new Uint8Array(size); const FILE_BUFFER_SIZE = 4096; const createFileBuffer = () => createBuffer(FILE_BUFFER_SIZE); -const pool = new ObjectPool(createFileBuffer, { size: 10, max: 15 }); -const instance = pool.acquire(); -console.log({ instance }); -pool.release(instance); +const pool = new ObjectPool(createFileBuffer, { size: 2, max: 4 }); + +pool.acquire((instance1) => { + console.log('Acquired instance 1', instance1); + setTimeout(() => { + console.log('Releasing instance 1'); + pool.release(instance1); + }, 1000); +}); + +pool.acquire((instance2) => { + console.log('Acquired instance 2', instance2); + setTimeout(() => { + console.log('Releasing instance 2'); + pool.release(instance2); + }, 1500); +}); + +pool.acquire((instance3) => { + console.log('Acquired instance 3', instance3); + setTimeout(() => { + console.log('Releasing instance 3'); + pool.release(instance3); + }, 1000); +}); + +pool.acquire((instance4) => { + console.log('Acquired instance 4', instance4); + setTimeout(() => { + console.log('Releasing instance 4'); + pool.release(instance4); + }, 1500); +}); From cf1efcd9aed0b556152faa6d79e896c75e5b19d5 Mon Sep 17 00:00:00 2001 From: Walik23 Date: Thu, 12 Jun 2025 14:57:51 +0300 Subject: [PATCH 5/6] task5: Rewrite acquisition to promises in closure version --- JavaScript/4-bad.js | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/JavaScript/4-bad.js b/JavaScript/4-bad.js index c602d68..59dd975 100644 --- a/JavaScript/4-bad.js +++ b/JavaScript/4-bad.js @@ -7,21 +7,23 @@ const poolify = (factory, size, max) => { const processQueue = () => { while (queue.length > 0 && available > 0){ - const { callback } = queue.shift(); - callback(instances.pop()); + const { resolve } = queue.shift(); + resolve(instances.pop()); available--; } }; - const acquire = (callback) => { - if(available > 0){ - const instance = instances.pop(); - available--; - callback(instance); - } - else { - queue.push({ callback }); - } + const acquire = () => { + return new Promise((resolve) => { + if(available > 0){ + const instance = instances.pop(); + available--; + resolve(instance); + } + else { + queue.push({ resolve }); + } + }); }; const release = (instance) => { @@ -46,34 +48,32 @@ const size = 2; const max = 4; const pool = poolify(createFileBuffer, size, max); -pool.acquire((instance1) => { +(async () => { + const instance1 = await pool.acquire(); console.log('Acquired instance 1', instance1); setTimeout(() => { console.log('Releasing instance 1'); pool.release(instance1); }, 1000); -}); -pool.acquire((instance2) => { + const instance2 = await pool.acquire(); console.log('Acquired instance 2', instance2); setTimeout(() => { console.log('Releasing instance 2'); pool.release(instance2); }, 1500); -}); -pool.acquire((instance3) => { + const instance3 = await pool.acquire(); console.log('Acquired instance 3', instance3); setTimeout(() => { console.log('Releasing instance 3'); pool.release(instance3); }, 1000); -}); -pool.acquire((instance4) => { + const instance4 = await pool.acquire(); console.log('Acquired instance 4', instance4); setTimeout(() => { console.log('Releasing instance 4'); pool.release(instance4); }, 1500); -}); +})(); \ No newline at end of file From f85d5a51f8d23c74807876dbfa9043af777d399c Mon Sep 17 00:00:00 2001 From: Walik23 Date: Thu, 12 Jun 2025 15:08:55 +0300 Subject: [PATCH 6/6] task6: Rewrite acquisition to promises in classes version --- JavaScript/4-improved.js | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/JavaScript/4-improved.js b/JavaScript/4-improved.js index 9a32aec..0271975 100644 --- a/JavaScript/4-improved.js +++ b/JavaScript/4-improved.js @@ -11,21 +11,23 @@ class ObjectPool { _processQueue() { while (this.queue.length > 0 && this.available > 0){ - const { callback } = this.queue.shift(); - callback(this.instances.pop()); + const { resolve } = this.queue.shift(); + resolve(this.instances.pop()); this.available--; } }; - acquire (callback) { - if(this.available > 0){ + acquire () { + return new Promise((resolve) => { + if(this.available > 0){ const instance = this.instances.pop(); this.available--; - callback(instance); - } - else{ - this.queue.push({ callback }); - } + resolve(instance); + } + else{ + this.queue.push({ resolve }); + } + }); }; release (instance) { @@ -46,34 +48,32 @@ const createFileBuffer = () => createBuffer(FILE_BUFFER_SIZE); const pool = new ObjectPool(createFileBuffer, { size: 2, max: 4 }); -pool.acquire((instance1) => { +(async () => { + const instance1 = await pool.acquire(); console.log('Acquired instance 1', instance1); setTimeout(() => { console.log('Releasing instance 1'); pool.release(instance1); }, 1000); -}); -pool.acquire((instance2) => { + const instance2 = await pool.acquire(); console.log('Acquired instance 2', instance2); setTimeout(() => { console.log('Releasing instance 2'); pool.release(instance2); }, 1500); -}); -pool.acquire((instance3) => { + const instance3 = await pool.acquire(); console.log('Acquired instance 3', instance3); setTimeout(() => { console.log('Releasing instance 3'); pool.release(instance3); }, 1000); -}); -pool.acquire((instance4) => { + const instance4 = await pool.acquire(); console.log('Acquired instance 4', instance4); setTimeout(() => { console.log('Releasing instance 4'); pool.release(instance4); }, 1500); -}); +})(); \ No newline at end of file