Skip to content

Instantly share code, notes, and snippets.

@JoseJuan81
Created May 15, 2019 16:19
Show Gist options
  • Save JoseJuan81/396647be7299fc7135b2b5ae923f249a to your computer and use it in GitHub Desktop.
Save JoseJuan81/396647be7299fc7135b2b5ae923f249a to your computer and use it in GitHub Desktop.
Estudio de velocidad de procesamiento en el navegador
// actualizar un item en una matriz
function createArr(n) {
var col = [];
for (var i = 1; i <= n; i++) {
col.push({id: i})
}
return col;
}
var n = 1000000
var arr = createArr(n); // crear una matriz con n elementos del tipo {id: 1}
function find(fn, col) {
return function innerFind(){
return col.find(fn);
};
}
function equality(prop, val) {
return function innerEquality(item) {
return item[prop] === val;
};
}
function setNewProperty(prop, val) {
return function innerSetNewProperty(item) {
item[prop] = val;
return item;
};
}
function findIndex(fn, col) {
return function innerFindIndex() {
return col.findIndex(fn);
}
}
function updateItemInCollection(item, col) {
return function inner(index) {
col[index] = item;
return col;
}
}
var compose = (...fns) => (...args) => fns.reduceRight((res, fn) => [fn.call(null, ...res)], args)[0];
var prop = 'id';
var val = 1000000;
// agregarle la propiedad 'age' al elemento con id = val y actualizar la matriz
console.time('compose')
var itemUpdated = compose(setNewProperty('age', 6), find(equality(prop, val), arr))();
var collUpdated = compose(updateItemInCollection(itemUpdated, arr), findIndex(equality(prop, val), arr))()
console.timeEnd('compose')
console.time('tradicional con map')
var collUpdatedTraditional = arr.map(item => {
var newItem = { ...item };
if (newItem.id === val){
newItem.age = 7;
}
return newItem;
})
console.timeEnd('tradicional con map')
console.time('tradicional index')
var index = arr.findIndex(item => item.id === val);
var newArr = [...arr];
newArr[index].age = 8;
console.timeEnd('tradicional index')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment