counting properties in array of objects

JavaScript performance comparison

Test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var counts = {};
  const features = [ 
  {"properties":{"name":"city1","zip":1234}, "geometry":{"type":"polygon","coordinates":[[1,2],[3,4]]}},
  {"properties":{"name":"city2","zip":1234}, "geometry":{"type":"polygon","coordinates":[[1,2],[3,4]]}},
  {"properties":{"name":"city3"},"geometry":{"type":"multiPolygon","coordinates":[[[1,2],[3,4]]]}},
  {"properties":{"name":"city4","zip":1234,"updated":"May-2018"}, "geometry":{"type":"polygon","coordinates":[]}},
  {"properties":{"name":"city5","zip":1234}, "geometry":{"type":"polygon","coordinates":[[1,2],[3,4]]}},
  {"properties":{"name":"city6","zip":1234}, "geometry":{"type":"polygon","coordinates":[[1,2],[3,4]]}},
  {"properties":{"name":"city7"},"geometry":{"type":"multiPolygon","coordinates":[[[1,2],[3,4]]]}},
  {"properties":{"name":"city1000","zip":1234,"updated":"May-2018"}, "geometry":{"type":"polygon","coordinates":[]}} ];
  const featurestring = '[ {"properties":{"name":"city1","zip":1234}, "geometry":{"type":"polygon","coordinates":[[1,2],[3,4]]}}, {"properties":{"name":"city2","zip":1234}, "geometry":{"type":"polygon","coordinates":[[1,2],[3,4]]}}, {"properties":{"name":"city3"},"geometry":{"type":"multiPolygon","coordinates":[[[1,2],[3,4]]]}}, {"properties":{"name":"city4","zip":1234,"updated":"May-2018"}, "geometry":{"type":"polygon","coordinates":[]}}, {"properties":{"name":"city5","zip":1234}, "geometry":{"type":"polygon","coordinates":[[1,2],[3,4]]}}, {"properties":{"name":"city6","zip":1234}, "geometry":{"type":"polygon","coordinates":[[1,2],[3,4]]}}, {"properties":{"name":"city7"},"geometry":{"type":"multiPolygon","coordinates":[[[1,2],[3,4]]]}}, {"properties":{"name":"city1000","zip":1234,"updated":"May-2018"}, "geometry":{"type":"polygon","coordinates":[]}} ]';
  

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
JSON.stringify
JSON.stringify(features, (k, v) => (isNaN(k) && (counts[k] = counts[k] + 1 || 1), v))
pending…
recursion.v1
const featuresCount = features.reduce((count, feature) => {
  Object.keys(feature).forEach(key => {
    count[key] = count[key] || 0;
    count[key] += 1;
    Object.keys(feature[key]).forEach(key2 => {
      let count2key = `${key}.${key2}`;
      count[count2key] = count[count2key] || 0;
      count[count2key] += 1;
    });
  });
  return count;
}, {});
pending…
JSON.parse(.stringify)
var fs = JSON.parse(JSON.stringify(features), (k, v) => (isNaN(k) && (counts[k] = counts[k] + 1 || 1), v))
pending…
initial
const lp1 = (ff) => ff.reduce((pf,f) => Array.from(new Set(pf.concat(Object.keys( f)))), []);
const lp2 = (ff) => ff.reduce((pf,f) => Array.from(new Set(pf.concat(Object.keys( f.properties)))), []);
const lp3 = (ff) => Object.keys(ff[0].geometry);
const cp = ff => pf => ` ${pf}:${ff.reduce((p,f)=> p+(!!f[pf]), 0)}`;
const pi = ff => pf => ` ${pf}:${ff.reduce((p,f)=> p+ (!!f.properties[pf]), 0)}`;
const pg = ff => pf => ` ${pf}:${ff.reduce((p,f)=> p+ (!!f.geometry[pf]), 0)}`;
let str = lp1(features).map((kk,i) => cp(features)(kk));
if (features[0].properties) {
		str += " prop."+ lp2(features).map(kk => ` ${kk}: ${pi(features)}`) +" geom."+ lp3(features).map(kk => ` ${kk}: ${pg(features)}`);
}
pending…
recursion.v2
const featuresCount = features.reduce((count, feature) => {
  Object.keys(feature).forEach(key => {
    if (count[key] === undefined) count[key] = 0;
    count[key] += 1;
    Object.keys(feature[key]).forEach(key2 => {
      let count2key = `${key}.${key2}`;
      if (count[count2key] === undefined) count[count2key] = 0;
      count[count2key] += 1;
    });
  });
  return count;
}, {});
pending…
JSON.parse
var fs = JSON.parse(featurestring, (k, v) => (isNaN(k) && (counts[k] = counts[k] + 1 || 1), v))
pending…

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

Compare results of other browsers

0 Comments