11cb0ef41Sopenharmony_ci'use strict'; 21cb0ef41Sopenharmony_ci 31cb0ef41Sopenharmony_ciconst common = require('../common.js'); 41cb0ef41Sopenharmony_ci 51cb0ef41Sopenharmony_ciconst bench = common.createBenchmark(main, { 61cb0ef41Sopenharmony_ci n: [100000], 71cb0ef41Sopenharmony_ci}, { 81cb0ef41Sopenharmony_ci flags: ['--expose-internals'], 91cb0ef41Sopenharmony_ci}); 101cb0ef41Sopenharmony_ci 111cb0ef41Sopenharmony_cifunction main({ n }) { 121cb0ef41Sopenharmony_ci let FreeList = require('internal/freelist'); 131cb0ef41Sopenharmony_ci if (FreeList.FreeList) 141cb0ef41Sopenharmony_ci FreeList = FreeList.FreeList; 151cb0ef41Sopenharmony_ci const poolSize = 1000; 161cb0ef41Sopenharmony_ci const list = new FreeList('test', poolSize, Object); 171cb0ef41Sopenharmony_ci let j; 181cb0ef41Sopenharmony_ci const used = []; 191cb0ef41Sopenharmony_ci 201cb0ef41Sopenharmony_ci // First, alloc `poolSize` items 211cb0ef41Sopenharmony_ci for (j = 0; j < poolSize; j++) { 221cb0ef41Sopenharmony_ci used.push(list.alloc()); 231cb0ef41Sopenharmony_ci } 241cb0ef41Sopenharmony_ci 251cb0ef41Sopenharmony_ci bench.start(); 261cb0ef41Sopenharmony_ci 271cb0ef41Sopenharmony_ci for (let i = 0; i < n; i++) { 281cb0ef41Sopenharmony_ci // Return all the items to the pool 291cb0ef41Sopenharmony_ci for (j = 0; j < poolSize; j++) { 301cb0ef41Sopenharmony_ci list.free(used[j]); 311cb0ef41Sopenharmony_ci } 321cb0ef41Sopenharmony_ci 331cb0ef41Sopenharmony_ci // Re-alloc from pool 341cb0ef41Sopenharmony_ci for (j = 0; j < poolSize; j++) { 351cb0ef41Sopenharmony_ci list.alloc(); 361cb0ef41Sopenharmony_ci } 371cb0ef41Sopenharmony_ci } 381cb0ef41Sopenharmony_ci 391cb0ef41Sopenharmony_ci bench.end(n); 401cb0ef41Sopenharmony_ci} 41