diff options
| author | Geo Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2024-04-17 18:32:13 +0300 |
|---|---|---|
| committer | Geo Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2024-04-17 18:32:13 +0300 |
| commit | d9364679a51ff80db8e5948ab089d749da36a6b2 (patch) | |
| tree | f847a03a017be8857923aaaae9f8dcf7177b2ba9 /benchmark | |
| parent | 6248d2cf7a2214ac32628bc58248694108b4d8bf (diff) | |
| download | oseine-d9364679a51ff80db8e5948ab089d749da36a6b2.tar.gz oseine-d9364679a51ff80db8e5948ab089d749da36a6b2.tar.bz2 oseine-d9364679a51ff80db8e5948ab089d749da36a6b2.zip | |
dockerize the appHEADmasterdevelopment
Diffstat (limited to 'benchmark')
| -rw-r--r-- | benchmark/find.js | 105 | ||||
| -rw-r--r-- | benchmark/match-str.js | 74 |
2 files changed, 0 insertions, 179 deletions
diff --git a/benchmark/find.js b/benchmark/find.js deleted file mode 100644 index 2d18657..0000000 --- a/benchmark/find.js +++ /dev/null @@ -1,105 +0,0 @@ -/** - * benchmark: find a product in product list - * using: for vs forEach vs find - */ - -const microtime = require('microtime'); - -const products = require('../data/products.json'); - - -var selected = []; -products.forEach( pr => { - if (Math.floor(Math.random() * 100) > 85) { - selected.push(pr.id); - } -}); - - -function compare() { - - let n = 4; - - var f0 = microtime.nowDouble(); - for(i=0 ; i < n ; i++) byFor(); - var f1 = microtime.nowDouble(); - // - var e0 = microtime.nowDouble(); - for(i=0 ; i < n ; i++) byEach(); - var e1 = microtime.nowDouble(); - // - var b0 = microtime.nowDouble(); - for(i=0 ; i < n ; i++) byFind(); - var b1 = microtime.nowDouble(); - - return { - n: n, - - for: f1-f0, - items_for: byFor(), - - each: e1-e0, - items_each: byEach(), - - find: b1-b0, - items_find: byFind(), - - sel: selected, - } -} - - -function byFor() { - var items = []; - var notFound = []; - var found; - selected.forEach( id => { - found = false; - for(i = 0; i < products.length; i++) { - if (products[i].id == id) { - items.push(products[i]); - found = true; - break; - } - } - if (!found) notFound.push(id) - - }) - return {items: items, nf: notFound}; -} - - -function byEach() { - var items = []; - var notFound = []; - var found; - selected.forEach( id => { - found = false; - products.forEach( pr => { - if (pr.id == id) { - items.push(pr); - found = true; - } - }); - if (!found) notFound.push(id) - }); - return {items: items, nf: notFound}; -} - -function byFind() { - var items = []; - var notFound = []; - var result; - selected.forEach( id => { - found = false; - result = products.find((pr) => pr.id == id); - if (result === undefined) notFound.push(id) - else items.push(result); - }); - return {items: items, nf: notFound}; -} - -module.exports = { - compare, - byFor -}
\ No newline at end of file diff --git a/benchmark/match-str.js b/benchmark/match-str.js deleted file mode 100644 index 2114f41..0000000 --- a/benchmark/match-str.js +++ /dev/null @@ -1,74 +0,0 @@ -var microtime = require('microtime'); - -const match = require('../utils/match-util.js'); -const memory_usage = require('../utils/mem-usage.js'); -const kb = require('../utils/kb-util.js'); - -const products = require('../data/products.json'); - -// memory_usage.report(); - -// test runner -function run(query) { - - var f0 = microtime.nowDouble(); - 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, - result: result - } -} - -// sample search implementation -function matchQuery(query) { - var result = []; - - let q = kb.keyboardize(query); - - for(let i = 0; i < products.length; i++) { - - let rate = 0; - let found = false; - let x; - let src = products[i].kb.split(' '); // (array) source of product (key-)words - if ((x = match.weighted_exact(q, src)) > 0) { - // console.log(products[i].kb, x); - rate = 5.0 + x; - found = true; - } - else if ((x = match.weighted_partial(q, src)) > 0) { - rate = 3.0 + x; - found = true; - } - else { - let similarity = 0; - src.forEach( w => { - let sim = match.resemblance(q, w, 2); - if (sim > 0.6) { - found = true; - similarity = (sim > similarity) ? sim : similarity; - } - }); - if (found) { - rate = 2 * similarity; - } - } - - if (found) { - products[i].rate = rate; - result.push(products[i]); - } - } - - return result.sort((a,b) => b.rate - a.rate).slice(0, 48);; -} - - -module.exports = { run } |
