summaryrefslogtreecommitdiff
path: root/app/benchmark/find.js
diff options
context:
space:
mode:
authorGeo Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2024-04-17 18:32:13 +0300
committerGeo Halkiadakis <gchalkiadakis@sklavenitis.co.gr>2024-04-17 18:32:13 +0300
commitd9364679a51ff80db8e5948ab089d749da36a6b2 (patch)
treef847a03a017be8857923aaaae9f8dcf7177b2ba9 /app/benchmark/find.js
parent6248d2cf7a2214ac32628bc58248694108b4d8bf (diff)
downloadoseine-development.tar.gz
oseine-development.tar.bz2
oseine-development.zip
dockerize the appHEADmasterdevelopment
Diffstat (limited to 'app/benchmark/find.js')
-rw-r--r--app/benchmark/find.js112
1 files changed, 112 insertions, 0 deletions
diff --git a/app/benchmark/find.js b/app/benchmark/find.js
new file mode 100644
index 0000000..1e86c24
--- /dev/null
+++ b/app/benchmark/find.js
@@ -0,0 +1,112 @@
+/**
+ * benchmark: find a product in product list
+ * using: for vs forEach vs find
+ */
+
+const microtime = require('microtime');
+
+var products = require('../../data/products.json');
+if (products.length == 0) {
+ const prepare = require('../pieces/prepare.js');
+ prepare.load_products(
+ 'https://storage.googleapis.com/pythia-files/uploads/json/emarket-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