From b122279ece06a9381e805c6087f130b41616fe3d Mon Sep 17 00:00:00 2001 From: Geo Halkiadakis Date: Fri, 12 Apr 2024 18:31:00 +0300 Subject: added utils folder; added several suplamentary modules --- benchmark/find.js | 27 ++++++++++++++++++++------- benchmark/match-str.js | 22 ++++++++++++++-------- 2 files changed, 34 insertions(+), 15 deletions(-) (limited to 'benchmark') diff --git a/benchmark/find.js b/benchmark/find.js index cc95a44..2d18657 100644 --- a/benchmark/find.js +++ b/benchmark/find.js @@ -1,5 +1,11 @@ +/** + * benchmark: find a product in product list + * using: for vs forEach vs find + */ + +const microtime = require('microtime'); + const products = require('../data/products.json'); -var microtime = require('microtime'); var selected = []; @@ -12,25 +18,32 @@ products.forEach( pr => { function compare() { + let n = 4; + var f0 = microtime.nowDouble(); - for(i=0 ; i < 4 ; i++) byFor(); + for(i=0 ; i < n ; i++) byFor(); var f1 = microtime.nowDouble(); // var e0 = microtime.nowDouble(); - for(i=0 ; i < 4 ; i++) byEach(); + for(i=0 ; i < n ; i++) byEach(); var e1 = microtime.nowDouble(); // var b0 = microtime.nowDouble(); - for(i=0 ; i < 4 ; i++) byFind(); + for(i=0 ; i < n ; i++) byFind(); var b1 = microtime.nowDouble(); return { + n: n, + for: f1-f0, - ifor: byFor(), + items_for: byFor(), + each: e1-e0, - ieach: byEach(), + items_each: byEach(), + find: b1-b0, - ifind: byFind(), + items_find: byFind(), + sel: selected, } } diff --git a/benchmark/match-str.js b/benchmark/match-str.js index 7760b4e..fa802c1 100644 --- a/benchmark/match-str.js +++ b/benchmark/match-str.js @@ -1,17 +1,23 @@ -const products = require('../data/products.json'); -const match = require('../pieces/match-util.js'); var microtime = require('microtime'); +const match = require('../utils/match-util.js'); +const memory_usage = require('../utils/mem-usage.js'); + +const products = require('../data/products.json'); -query = 'solokata'; +// memory_usage.report(); -function run() { +// test runner +function run(query) { var f0 = microtime.nowDouble(); - // for(let i = 0 ; i < 100 ; i++) - let result = matchQuery(query); + let result; + // for(let i = 0 ; i < 20 ; i++) + result = matchQuery(query); var f1 = microtime.nowDouble(); + memory_usage.report(); + return { t: f1-f0, q: query, @@ -19,7 +25,7 @@ function run() { } } - +// sumple search implementation function matchQuery(q) { var result = []; @@ -43,4 +49,4 @@ function matchQuery(q) { } -module.exports = { run } \ No newline at end of file +module.exports = { run } -- cgit v1.2.3