Skip to content

Instantly share code, notes, and snippets.

@entrptaher
Last active April 6, 2021 08:57
Show Gist options
  • Save entrptaher/c3e3b0053eaf78e239f0d0f756d90517 to your computer and use it in GitHub Desktop.
Save entrptaher/c3e3b0053eaf78e239f0d0f756d90517 to your computer and use it in GitHub Desktop.
Imperative vs Declarative #jsbench #jsperf (http://jsbench.github.io/#c3e3b0053eaf78e239f0d0f756d90517) #jsbench #jsperf
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8"/>
<title>Imperative vs Declarative #jsbench #jsperf</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>
"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 result = []
for (var i = 1; i< 10000; i++){
result.push(i);
}
// fast.js via browserify: https://wzrd.in/standalone/fast.js@latest
!function(t){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var r;r="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,(r.fast||(r.fast={})).js=t()}}(function(){return function t(r,n,e){function o(u,c){if(!n[u]){if(!r[u]){var a="function"==typeof require&&require;if(!c&&a)return a(u,!0);if(i)return i(u,!0);var f=new Error("Cannot find module '"+u+"'");throw f.code="MODULE_NOT_FOUND",f}var s=n[u]={exports:{}};r[u][0].call(s.exports,function(t){var n=r[u][1][t];return o(n?n:t)},s,s.exports,t,r,n,e)}return n[u].exports}for(var i="function"==typeof require&&require,u=0;u<e.length;u++)o(e[u]);return o}({1:[function(t,r,n){"use strict";r.exports=function(t){var r,n=t.length,e=new Array(n);for(r=0;r<n;r++)e[r]=t[r];return e}},{}],2:[function(t,r,n){"use strict";r.exports=function(){var t,r,n,e,o=arguments.length,i=[];for(t=0;t<o;t++)if(r=arguments[t],Array.isArray(r))for(n=r.length,e=0;e<n;e++)i.push(r[e]);else i.push(r);return i}},{}],3:[function(t,r,n){"use strict";var e=t("../function/bindInternal3");r.exports=function(t,r,n){var o,i=t.length,u=void 0!==n?e(r,n):r;for(o=0;o<i;o++)if(!u(t[o],o,t))return!1;return!0}},{"../function/bindInternal3":22}],4:[function(t,r,n){"use strict";r.exports=function(t,r,n,e){var o,i=t.length;for(void 0===n&&(n=0),void 0===e&&(e=i),o=n;o<e;o++)t[o]=r;return t}},{}],5:[function(t,r,n){"use strict";var e=t("../function/bindInternal3");r.exports=function(t,r,n){var o,i=t.length,u=[],c=void 0!==n?e(r,n):r;for(o=0;o<i;o++)c(t[o],o,t)&&u.push(t[o]);return u}},{"../function/bindInternal3":22}],6:[function(t,r,n){"use strict";var e=t("../function/bindInternal3");r.exports=function(t,r,n){var o,i=t.length,u=void 0!==n?e(r,n):r;for(o=0;o<i;o++)u(t[o],o,t)}},{"../function/bindInternal3":22}],7:[function(t,r,n){"use strict";n.clone=t("./clone"),n.concat=t("./concat"),n.every=t("./every"),n.filter=t("./filter"),n.forEach=t("./forEach"),n.indexOf=t("./indexOf"),n.lastIndexOf=t("./lastIndexOf"),n.map=t("./map"),n.pluck=t("./pluck"),n.reduce=t("./reduce"),n.reduceRight=t("./reduceRight"),n.some=t("./some"),n.fill=t("./fill")},{"./clone":1,"./concat":2,"./every":3,"./fill":4,"./filter":5,"./forEach":6,"./indexOf":8,"./lastIndexOf":9,"./map":10,"./pluck":11,"./reduce":12,"./reduceRight":13,"./some":14}],8:[function(t,r,n){"use strict";r.exports=function(t,r,n){var e=t.length,o=0;for("number"==typeof n&&(o=n,o<0&&(o+=e,o<0&&(o=0)));o<e;o++)if(t[o]===r)return o;return-1}},{}],9:[function(t,r,n){"use strict";r.exports=function(t,r,n){var e=t.length,o=e-1;for("number"==typeof n&&(o=n,o<0&&(o+=e));o>=0;o--)if(t[o]===r)return o;return-1}},{}],10:[function(t,r,n){"use strict";var e=t("../function/bindInternal3");r.exports=function(t,r,n){var o,i=t.length,u=new Array(i),c=void 0!==n?e(r,n):r;for(o=0;o<i;o++)u[o]=c(t[o],o,t);return u}},{"../function/bindInternal3":22}],11:[function(t,r,n){"use strict";r.exports=function(t,r){var n,e,o=t.length,i=[],u=0;for(e=0;e<o;e++)n=t[e],null!=n&&void 0!==n[r]&&(i[u++]=n[r]);return i}},{}],12:[function(t,r,n){"use strict";var e=t("../function/bindInternal4");r.exports=function(t,r,n,o){var i,u,c=t.length,a=void 0!==o?e(r,o):r;for(void 0===n?(i=1,u=t[0]):(i=0,u=n);i<c;i++)u=a(u,t[i],i,t);return u}},{"../function/bindInternal4":23}],13:[function(t,r,n){"use strict";var e=t("../function/bindInternal4");r.exports=function(t,r,n,o){var i,u,c=t.length,a=void 0!==o?e(r,o):r;for(void 0===n?(i=c-2,u=t[c-1]):(i=c-1,u=n);i>=0;i--)u=a(u,t[i],i,t);return u}},{"../function/bindInternal4":23}],14:[function(t,r,n){"use strict";var e=t("../function/bindInternal3");r.exports=function(t,r,n){var o,i=t.length,u=void 0!==n?e(r,n):r;for(o=0;o<i;o++)if(u(t[o],o,t))return!0;return!1}},{"../function/bindInternal3":22}],15:[function(t,r,n){"use strict";var e=t("./array/clone"),o=t("./object/clone");r.exports=function(t){return t&&"object"==typeof t?Array.isArray(t)?e(t):o(t):t}},{"./array/clone":1,"./object/clone":31}],16:[function(t,r,n){"use strict";var e=t("./array/filter"),o=t("./object/filter");r.exports=function(t,r,n){return t instanceof Array?e(t,r,n):o(t,r,n)}},{"./array/filter":5,"./object/filter":32}],17:[function(t,r,n){"use strict";var e=t("./array/forEach"),o=t("./object/forEach");r.exports=function(t,r,n){return t instanceof Array?e(t,r,n):o(t,r,n)}},{"./array/forEach":6,"./object/forEach":33}],18:[function(t,r,n){"use strict";var e=t("./applyWithContext"),o=t("./applyNoContext");r.exports=function(t,r,n){return void 0!==r?e(t,r,n):o(t,n)}},{"./applyNoContext":19,"./applyWithContext":20}],19:[function(t,r,n){"use strict";r.exports=function(t,r){switch(r.length){case 0:return t();case 1:return t(r[0]);case 2:return t(r[0],r[1]);case 3:return t(r[0],r[1],r[2]);case 4:return t(r[0],r[1],r[2],r[3]);case 5:return t(r[0],r[1],r[2],r[3],r[4]);case 6:return t(r[0],r[1],r[2],r[3],r[4],r[5]);case 7:return t(r[0],r[1],r[2],r[3],r[4],r[5],r[6]);case 8:return t(r[0],r[1],r[2],r[3],r[4],r[5],r[6],r[7]);default:return t.apply(void 0,r)}}},{}],20:[function(t,r,n){"use strict";r.exports=function(t,r,n){switch(n.length){case 0:return t.call(r);case 1:return t.call(r,n[0]);case 2:return t.call(r,n[0],n[1]);case 3:return t.call(r,n[0],n[1],n[2]);case 4:return t.call(r,n[0],n[1],n[2],n[3]);case 5:return t.call(r,n[0],n[1],n[2],n[3],n[4]);case 6:return t.call(r,n[0],n[1],n[2],n[3],n[4],n[5]);case 7:return t.call(r,n[0],n[1],n[2],n[3],n[4],n[5],n[6]);case 8:return t.call(r,n[0],n[1],n[2],n[3],n[4],n[5],n[6],n[7]);default:return t.apply(r,n)}}},{}],21:[function(t,r,n){"use strict";var e=t("./applyWithContext"),o=t("./applyNoContext");r.exports=function(t,r){var n,i=arguments.length-2;if(i>0){n=new Array(i);for(var u=0;u<i;u++)n[u]=arguments[u+2];return void 0!==r?function(){var o,u=arguments.length,c=new Array(i+u);for(o=0;o<i;o++)c[o]=n[o];for(o=0;o<u;o++)c[i+o]=arguments[o];return e(t,r,c)}:function(){var r,e=arguments.length,u=new Array(i+e);for(r=0;r<i;r++)u[r]=n[r];for(r=0;r<e;r++)u[i+r]=arguments[r];return o(t,u)}}return void 0!==r?function(){return e(t,r,arguments)}:function(){return o(t,arguments)}}},{"./applyNoContext":19,"./applyWithContext":20}],22:[function(t,r,n){"use strict";r.exports=function(t,r){return function(n,e,o){return t.call(r,n,e,o)}}},{}],23:[function(t,r,n){"use strict";r.exports=function(t,r){return function(n,e,o,i){return t.call(r,n,e,o,i)}}},{}],24:[function(t,r,n){"use strict";n.apply=t("./apply"),n.bind=t("./bind"),n.partial=t("./partial"),n.partialConstructor=t("./partialConstructor"),n.try=t("./try")},{"./apply":18,"./bind":21,"./partial":25,"./partialConstructor":26,"./try":27}],25:[function(t,r,n){"use strict";var e=t("./applyWithContext");r.exports=function(t){var r,n=arguments.length-1;r=new Array(n);for(var o=0;o<n;o++)r[o]=arguments[o+1];return function(){var o,i=arguments.length,u=new Array(n+i);for(o=0;o<n;o++)u[o]=r[o];for(o=0;o<i;o++)u[n+o]=arguments[o];return e(t,this,u)}}},{"./applyWithContext":20}],26:[function(t,r,n){"use strict";var e=t("./applyWithContext");r.exports=function(t){var r,n=arguments.length-1;r=new Array(n);for(var o=0;o<n;o++)r[o]=arguments[o+1];return function(){var o,i=arguments.length,u=new Array(n+i);for(o=0;o<n;o++)u[o]=r[o];for(o=0;o<i;o++)u[n+o]=arguments[o];var c=Object.create(t.prototype),a=e(t,c,u);return null==a||"object"!=typeof a&&"function"!=typeof a?c:a}}},{"./applyWithContext":20}],27:[function(t,r,n){"use strict";r.exports=function(t){try{return t()}catch(t){return t instanceof Error?t:new Error(t)}}},{}],28:[function(t,r,n){"use strict";function e(t){return this instanceof e?void(this.value=t||[]):new e(t)}r.exports=n=e,e.array=t("./array"),e.function=e.fn=t("./function"),e.object=t("./object"),e.string=t("./string"),e.apply=e.function.apply,e.bind=e.function.bind,e.partial=e.function.partial,e.partialConstructor=e.function.partialConstructor,e.try=e.attempt=e.function.try,e.assign=e.object.assign,e.cloneObject=e.object.clone,e.keys=e.object.keys,e.values=e.object.values,e.clone=t("./clone"),e.map=t("./map"),e.filter=t("./filter"),e.forEach=t("./forEach"),e.reduce=t("./reduce"),e.reduceRight=t("./reduceRight"),e.cloneArray=e.array.clone,e.concat=e.array.concat,e.some=e.array.some,e.every=e.array.every,e.indexOf=e.array.indexOf,e.lastIndexOf=e.array.lastIndexOf,e.pluck=e.array.pluck,e.fill=e.array.fill,e.intern=e.string.intern,e.prototype.concat=function(){var t,r,n,o,i=this.value.length,u=new Array(i);for(t=0;t<i;t++)u[t]=this.value[t];for(i=arguments.length,t=0;t<i;t++)if(r=arguments[t],Array.isArray(r))for(n=r.length,o=0;o<n;o++)u.push(r[o]);else u.push(r);return new e(u)},e.prototype.map=function(t,r){return new e(e.map(this.value,t,r))},e.prototype.filter=function(t,r){return new e(e.filter(this.value,t,r))},e.prototype.reduce=function(t,r,n){return e.reduce(this.value,t,r,n)},e.prototype.reduceRight=function(t,r,n){return e.reduceRight(this.value,t,r,n)},e.prototype.forEach=function(t,r){return e.forEach(this.value,t,r),this},e.prototype.some=function(t,r){return e.some(this.value,t,r)},e.prototype.every=function(t,r){return e.some(this.value,t,r)},e.prototype.indexOf=function(t,r){return e.indexOf(this.value,t,r)},e.prototype.lastIndexOf=function(t,r){return e.lastIndexOf(this.value,t,r)},e.prototype.reverse=function(){return new e(this.value.reverse())},e.prototype.valueOf=function(){return this.value},e.prototype.toJSON=function(){return this.value},Object.defineProperty(e.prototype,"length",{get:function(){return this.value.length}})},{"./array":7,"./clone":15,"./filter":16,"./forEach":17,"./function":24,"./map":29,"./object":34,"./reduce":40,"./reduceRight":41,"./string":42}],29:[function(t,r,n){"use strict";var e=t("./array/map"),o=t("./object/map");r.exports=function(t,r,n){return t instanceof Array?e(t,r,n):o(t,r,n)}},{"./array/map":10,"./object/map":36}],30:[function(t,r,n){"use strict";r.exports=function(t){var r,n,e,o,i,u,c=arguments.length;for(n=1;n<c;n++)for(r=arguments[n],o=Object.keys(r),e=o.length,u=0;u<e;u++)i=o[u],t[i]=r[i];return t}},{}],31:[function(t,r,n){"use strict";r.exports=function(t){var r,n,e=Object.keys(t),o=e.length,i={};for(r=0;r<o;r++)n=e[r],i[n]=t[n];return i}},{}],32:[function(t,r,n){"use strict";var e=t("../function/bindInternal3");r.exports=function(t,r,n){var o,i,u=Object.keys(t),c=u.length,a={},f=void 0!==n?e(r,n):r;for(o=0;o<c;o++)i=u[o],f(t[i],i,t)&&(a[i]=t[i]);return a}},{"../function/bindInternal3":22}],33:[function(t,r,n){"use strict";var e=t("../function/bindInternal3");r.exports=function(t,r,n){var o,i,u=Object.keys(t),c=u.length,a=void 0!==n?e(r,n):r;for(i=0;i<c;i++)o=u[i],a(t[o],o,t)}},{"../function/bindInternal3":22}],34:[function(t,r,n){"use strict";n.assign=t("./assign"),n.clone=t("./clone"),n.filter=t("./filter"),n.forEach=t("./forEach"),n.map=t("./map"),n.reduce=t("./reduce"),n.reduceRight=t("./reduceRight"),n.keys=t("./keys"),n.values=t("./values")},{"./assign":30,"./clone":31,"./filter":32,"./forEach":33,"./keys":35,"./map":36,"./reduce":37,"./reduceRight":38,"./values":39}],35:[function(t,r,n){"use strict";r.exports="function"==typeof Object.keys?Object.keys:function(t){var r=[];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&r.push(n);return r}},{}],36:[function(t,r,n){"use strict";var e=t("../function/bindInternal3");r.exports=function(t,r,n){var o,i,u=Object.keys(t),c=u.length,a={},f=void 0!==n?e(r,n):r;for(o=0;o<c;o++)i=u[o],a[i]=f(t[i],i,t);return a}},{"../function/bindInternal3":22}],37:[function(t,r,n){"use strict";var e=t("../function/bindInternal4");r.exports=function(t,r,n,o){var i,u,c,a=Object.keys(t),f=a.length,s=void 0!==o?e(r,o):r;for(void 0===n?(i=1,c=t[a[0]]):(i=0,c=n);i<f;i++)u=a[i],c=s(c,t[u],u,t);return c}},{"../function/bindInternal4":23}],38:[function(t,r,n){"use strict";var e=t("../function/bindInternal4");r.exports=function(t,r,n,o){var i,u,c,a=Object.keys(t),f=a.length,s=void 0!==o?e(r,o):r;for(void 0===n?(i=f-2,c=t[a[f-1]]):(i=f-1,c=n);i>=0;i--)u=a[i],c=s(c,t[u],u,t);return c}},{"../function/bindInternal4":23}],39:[function(t,r,n){"use strict";r.exports=function(t){for(var r=Object.keys(t),n=r.length,e=new Array(n),o=0;o<n;o++)e[o]=t[r[o]];return e}},{}],40:[function(t,r,n){"use strict";var e=t("./array/reduce"),o=t("./object/reduce");r.exports=function(t,r,n,i){return t instanceof Array?e(t,r,n,i):o(t,r,n,i)}},{"./array/reduce":12,"./object/reduce":37}],41:[function(t,r,n){"use strict";var e=t("./array/reduceRight"),o=t("./object/reduceRight");r.exports=function(t,r,n,i){return t instanceof Array?e(t,r,n,i):o(t,r,n,i)}},{"./array/reduceRight":13,"./object/reduceRight":38}],42:[function(t,r,n){"use strict";n.intern=t("./intern")},{"./intern":43}],43:[function(t,r,n){"use strict";var e={"- ":!0};delete e["- "],r.exports=function(t){e[t]=!0;var r=Object.keys(e)[0];return delete e[r],r}},{}]},{},[28])(28)});
const fastJS = fast.js;
};
suite.add("triple the value of every element in a given array", function () {
// triple the value of every element in a given array
const triple = (arr) => {
let results = []
for (let i = 0; i < arr.length; i++){
results.push(arr[i] * 3)
}
return results
}
// sum all the elements in a given array
const sum = (arr) => {
let result = 0
for (let i = 0; i < arr.length; i++){
result += arr[i]
}
return result
}
sum(triple(result))
});
suite.add("triple the value of every item in a given array", function () {
// triple the value of every item in a given array
const triple = (arr) => arr.map((currentItem) => currentItem * 3)
// sum all the elements in a given array
const sum = (arr) => arr.reduce((prev, current) => prev + current, 0)
sum(triple(result))
});
suite.add("triple the value of every item in a given array", function () {
// triple the value of every item in a given array
const triple = (arr) => fastJS.map(arr, (currentItem) => currentItem * 3)
// sum all the elements in a given array
const sum = (arr) => fastJS.reduce(arr,(prev, current) => prev + current, 0)
sum(triple(result))
});
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("Imperative vs Declarative #jsbench #jsperf");
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