JSFiddle - React, Tailwind, and code Playground
by EwertonRSilveira
HTML
<pre id="output"></pre>
JavaScript
'use strict';
/**
* Here are two smaller data sets to make it easy to check basic correctness.
*/
const userNames = [{
id: '1',
name: 'james'
//,description:null
}, {
id: '2',
name: 'sarah'
}, {
id: '3',
name: 'Marco'
}];
const userDescriptions = [{
id: '1',
description: 'a'
}, {
id: '3',
description: 'b'
}];
function mergeData(names, descriptions) {
//var timespan = new Date();
//console.log(timespan);
//console.log(timespan.getMinutes() + ':' + timespan.getSeconds());
var end, start;
start = new Date();
if (names && descriptions) {
descriptions.forEach(findElement);
};
function hasMatchingRecords(element, index, array) {
names.forEach(function(elm, idx, list) {
if (elm.id === element.id) {
list[idx].description = element.description;
array.splice(index, 1);
return;
};
});
};
function findElement(element, index, array) {
names.find(function(elm) {
if (elm.id === element.id) {
elm.description = element.description
return true;
};
return false;
});
};
end = new Date();
console.log('Operation took ' + (end.getTime() - start.getTime()) + ' msec');
return names;
};
function addDummyData(list, name) {
for (var i = 4; i < 10002; i++) {
list.push({
id: i.toString(),
name: name + i
});
};
console.log('New array Size', list.length);
};
function addDummyData(list, name, desc) {
for (var i = 4; i < 10002; i++) {
list.push({
id: i.toString(),
name: name + i,
description: desc+i
});
};
console.log('New array Size', list.length);
};
function mergeDataPowerTurbo(names, descriptions) {
var end, start;
start = new Date();
var result = names
.concat(descriptions)
.sort(function(elm){
return elm.id
});
end = new Date();
console.log('Operation took ' + (end.getTime() - start.getTime()) + ' msec');
...