summaryrefslogtreecommitdiff
path: root/benchmark/find.js
diff options
context:
space:
mode:
authorGeo Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2024-04-10 19:17:47 +0300
committerGeo Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2024-04-10 19:17:47 +0300
commitf6682b3a1d90ae4f0b86edebb28ebb17a811b57a (patch)
treefada5ca63432aff43e04511e2878ca0704af4349 /benchmark/find.js
parent54048a674e89fb90deb9766433c1dc01c8924c5b (diff)
downloadoseine-f6682b3a1d90ae4f0b86edebb28ebb17a811b57a.tar.gz
oseine-f6682b3a1d90ae4f0b86edebb28ebb17a811b57a.tar.bz2
oseine-f6682b3a1d90ae4f0b86edebb28ebb17a811b57a.zip
compare supplamentary search core implementations (for vs find, etc)
Diffstat (limited to 'benchmark/find.js')
-rw-r--r--benchmark/find.js92
1 files changed, 92 insertions, 0 deletions
diff --git a/benchmark/find.js b/benchmark/find.js
new file mode 100644
index 0000000..cc95a44
--- /dev/null
+++ b/benchmark/find.js
@@ -0,0 +1,92 @@
+const products = require('../data/products.json');
+var microtime = require('microtime');
+
+
+var selected = [];
+products.forEach( pr => {
+ if (Math.floor(Math.random() * 100) > 85) {
+ selected.push(pr.id);
+ }
+});
+
+
+function compare() {
+
+ var f0 = microtime.nowDouble();
+ for(i=0 ; i < 4 ; i++) byFor();
+ var f1 = microtime.nowDouble();
+ //
+ var e0 = microtime.nowDouble();
+ for(i=0 ; i < 4 ; i++) byEach();
+ var e1 = microtime.nowDouble();
+ //
+ var b0 = microtime.nowDouble();
+ for(i=0 ; i < 4 ; i++) byFind();
+ var b1 = microtime.nowDouble();
+
+ return {
+ for: f1-f0,
+ ifor: byFor(),
+ each: e1-e0,
+ ieach: byEach(),
+ find: b1-b0,
+ ifind: 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