Skip to content

Instantly share code, notes, and snippets.

@tj
Created January 6, 2011 03:32
Show Gist options
  • Save tj/767463 to your computer and use it in GitHub Desktop.
Save tj/767463 to your computer and use it in GitHub Desktop.
V8 hidden class benchmark
// 10m
var times = 10000000;
var n = times
, start = new Date;
while (n--) {
var point = [50,100];
point[0];
point[1];
}
console.log('array: %sms', new Date - start);
function Point(x, y){
this.x = x;
this.y = y;
}
var n = times
, start = new Date;
while (n--) {
var point = new Point(50, 100);
point.x;
point.y;
}
console.log('Point: %sms', new Date - start);
var n = times
, start = new Date;
while (n--) {
var point = { x: 50, y: 100 };
point.x;
point.y;
}
console.log('Object: %sms', new Date - start);
array: 161ms
Point: 135ms
Object: 275ms
C Point: 21.6ms
#include <stdio.h>
#include <time.h>
typedef struct {
double x;
double y;
} Point;
int
main(int argc, const char **argv){
int n = 10000000;
clock_t start = clock();
while (n--){
Point p = { 50, 100 };
p.x;
p.y;
}
printf("C Point: %.2fms\n", ((float)(clock() - start) / CLOCKS_PER_SEC) * 1000);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment