Last active
February 21, 2020 09:23
-
-
Save wooandoo/adcdfc66f12426306beaca73c4f44dce to your computer and use it in GitHub Desktop.
query factory (http://jsbench.github.io/#adcdfc66f12426306beaca73c4f44dce) #jsbench #jsperf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>query factory</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
const any = condition => values => { | |
console.log('any', condition, values) | |
for(let index = 0; index < values.length; index++) { | |
if (condition(values[index])) | |
return true | |
} | |
return false | |
} | |
const all_ = mapper => { | |
let _array = [] | |
const _or_wheres = [] | |
let _where = undefined | |
const andWhere = condition => { | |
const previous_condition = _where | |
_where = previous_condition === undefined | |
? condition | |
: item => previous_condition(item) && condition(item) | |
return factory | |
} | |
const factory = { | |
from: array => { | |
return _where === undefined | |
? array.map(mapper) | |
// : array.filter(item => any(condition => condition(item), [..._or_wheres, _where])).map(mapper) | |
: array.filter(item => { | |
if (_where(item)) | |
return true; | |
for (let index = 0; index < _or_wheres.length; index++) { | |
if (_or_wheres[index](item)) | |
return true; | |
} | |
return false | |
}) | |
}, | |
where: andWhere, | |
and: andWhere, | |
orWhere: condition => { | |
_or_wheres.push(_where) | |
_where = condition | |
return factory | |
} | |
} | |
return factory | |
} | |
const query = all_(item => item + 1) | |
.where(item => item % 2 === 0) | |
.and(item => item < 2) | |
.orWhere(item => item === 4 || item === 5) | |
.and(item => item % 2 === 0) | |
const demo = () => query.execute() | |
const demo2 = () => { | |
return ( | |
[0, 1, 2, 3, 4, 5] | |
.filter(item => ( | |
item % 2 === 0 && item < 2 | |
) || ( | |
item === 4 || item === 5 && item % 2 === 0) | |
) | |
.map(item => item + 1) | |
) | |
} | |
const demo_eval = eval(`array => array | |
.filter(item => ( | |
item % 2 === 0 && item < 2 | |
) || ( | |
item === 4 || item === 5 && item % 2 === 0) | |
) | |
.map(item => item + 1)`) | |
}; | |
suite.add("all_(item => item + 1)", function () { | |
all_(item => item + 1) | |
.where(item => item % 2 === 0) | |
.and(item => item < 2) | |
.orWhere(item => item === 4 || item === 5) | |
.and(item => item % 2 === 0) | |
.from([0, 1, 2, 3, 4, 5]) | |
}); | |
suite.add("query.from([0, 1, 2, 3, 4, 5])", function () { | |
query.from([0, 1, 2, 3, 4, 5]) | |
}); | |
suite.add("demo2()", function () { | |
demo2() | |
}); | |
suite.add("demo_eval([0, 1, 2, 3, 4, 5])", function () { | |
demo_eval([0, 1, 2, 3, 4, 5]) | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("query factory"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment