Extract attributes from an object (v3)

Revision 3 of this benchmark created on


Setup

const row = {
  "accelerations": 0,
  "average_speed": 27.6,
  "brakes": 2,
  "display_name": "MOV10 (MOV10)",
  "distance": 263.01,
  "driver_name": "PABLO VALENZUELA FRANCO",
  "hours_liter": 0.4310344827586206896551724138,
  "idle_hours": 0.20,
  "interval_hours": 105.8,
  "km_liter": 11.63,
  "liters": 22.62000,
  "liters_100km": 8.60,
  "liters_hour": 2.32,
  "movement_hours": 9.53,
  "refuel_date": "2023-05-29 18:39",
  "total_hours": 9.73,
  "total_pos_count": 897,
  "total_price": 200160.0000,
  "urban_percent": 83.0,
  "urban_pos_count": 749,
  "vehicle_description": "HDT-993, FIAT PALIO"
};
const fields = ['accelerations', 'liters', 'total_hours'];

Test runner

Ready to run.

Testing in
TestOps/sec
reduce (concat)
fields.reduce((acc, field) => {
  return acc.concat(row[field]);
}, []);
ready
reduce (single array)
fields.reduce((acc, field) => {
  acc.push(row[field]); 
  return acc;
}, []);
ready
forEach
const acc = [];
fields.forEach(field => {
  acc.push(row[field]);	
});
ready
Destructuring
const { accelerations, liters, total_hours } = row;
return [accelerations, liters, total_hours];
ready
Stringify
Object.values(JSON.parse(JSON.stringify(row, fields)));
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.