Test name | Executions per second |
---|---|
reduce | 163.2 Ops/sec |
reduce2 | 6.4 Ops/sec |
<script src=''></script>
var testData = {
keys: ["ID", "ID_CONTACT", "ID_CHAT", "DATE", "TEXT", "BLOB", "$HDN_BIN_BLOB", "HAS_BLOB", "BLOB_TYPE", "BLOB_DURATION", "BLOB_SIZE", "$HDN_DateTime", "$HDN_XCoord", "$HDN_YCoord", "$HDN_ZCoord", "$PK", "$HDN_IDState", "$HDN_DescState", "$HDN_NumBlob"],
values: []
};
for (var i = 0; i < 10000; i++) {
testData.values.push([
"ID",
"ID_CONTACT" + i,
"ID_CHAT" + i,
"DATE" + i,
"TEXT" + i,
"BLOB" + i,
"$HDN_BIN_BLOB" + i,
"HAS_BLOB" + i,
"BLOB_TYPE" + i,
"BLOB_DURATION" + i,
"BLOB_SIZE" + i,
"$HDN_DateTime" + i,
"$HDN_XCoord" + i,
"$HDN_YCoord" + i,
"$HDN_ZCoord" + i,
"$PK" + i,
"$HDN_IDState" + i,
"$HDN_DescState" + i,
"$HDN_NumBlob" + i
]);
}
var customReduce = data => {
var keys = data.keys;
return data.values.map(x => {
return x.reduce((acc, curr, i) => {
acc[keys[i]] = curr;
return acc;
}, {})
})
}
customReduce(testData)
function makeAnObject(data) {
return data.values.reduce((arr, vals) => {
return [
arr,
vals.reduce((obj, v, i) => ({ obj, [data.keys[i]]: v }), {})
]
}, [])
}
makeAnObject(testData)