Skip to content

Instantly share code, notes, and snippets.

@Grassboy
Created June 26, 2019 08:14
Show Gist options
  • Save Grassboy/05a6e815b8c010d64fa3e898b737a7b3 to your computer and use it in GitHub Desktop.
Save Grassboy/05a6e815b8c010d64fa3e898b737a7b3 to your computer and use it in GitHub Desktop.
which_is_faster.js
console.time('p1');
var data, n = 1000000;
for(var i = 0 ; i < n; i ++) data = { foo: 123, bar: i };
console.timeEnd('p1');
console.time('p2');
for(var i = 0 ; i < n; i ++) data = JSON.parse("{\"foo\":123,\"bar\":"+i+"}");
console.timeEnd('p2');
//p1: 872ms - 計時器停止
//p2: 1900ms - 計時器停止
console.time('p1');
var data, n = 1000000;
for(var i = 0 ; i < n; i ++) data = { foo: 123, bar: 456 };
console.timeEnd('p1');
console.time('p2');
for(var i = 0 ; i < n; i ++) data = JSON.parse("{\"foo\":123,\"bar\":456}");
console.timeEnd('p2');
//p1: 718ms - 計時器停止
//p2: 1299ms - 計時器停止
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment