Run details:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.61 Safari/537.36
Chrome 83
Windows
Desktop
4 years ago
Test name Executions per second
reduce 163.2 Ops/sec
reduce2 6.4 Ops/sec
HTML Preparation code:
x
 
1
2
<script src=''></script>
Script Preparation code:
 
 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
    ]);
  }
Tests:
  • reduce

     
    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)
  • reduce2

     
    function makeAnObject(data) {
      return data.values.reduce((arr, vals) => {
        return [
          ...arr,
          vals.reduce((obj, v, i) => ({ ...obj, [data.keys[i]]: v }), {})
        ]
      }, [])
    }
    makeAnObject(testData)