Skip to content

Instantly share code, notes, and snippets.

@hns
Created July 30, 2011 13:33
Show Gist options
  • Save hns/1115532 to your computer and use it in GitHub Desktop.
Save hns/1115532 to your computer and use it in GitHub Desktop.
micro-benchmark for property access
function bench(x) {
for (var i = 0; i < 10000000; i++) {
if (x.foo) x.baz;
if (x.bar) x.mur;
if (x.dng) x.foo;
if (x.baz) x.bar;
if (x.mur) x.dng;
}
}
if (typeof console === "undefined") console = {log: print};
var obj = {
foo: "foo",
bar: "bar",
dng: "dng",
baz: "baz",
mur: "mur"
};
for (var k = 0; k < 10; k++) {
var start = new Date().getTime();
bench(obj);
console.log(new Date().getTime() - start);
}
hannes@t61:~/git/rhino$ node -v
v0.5.3-pre
hannes@t61:~/git/rhino$ node getprop.js
740
738
738
739
738
737
740
738
738
738
hannes@t61:~/git/rhino$ java -version
java version "1.7.0"
Java(TM) SE Runtime Environment (build 1.7.0-b147)
Java HotSpot(TM) Server VM (build 21.0-b17, mixed mode)
hannes@t61:~/git/rhino$ java -jar build/rhino1_7R3/js.jar getprop.js
817
748
742
783
730
730
730
730
729
733
@micklev
Copy link

micklev commented Aug 2, 2011

what the interesting results, cool =) Hannes, could you write blog post how you achive that?

@micklev
Copy link

micklev commented Aug 2, 2011

really Hannes, how do you reach those numbers? what hardware/OS do you use? jdk 32 or 64 bit?
I'm tried to repeat this test and I've got numbers between 8093 and 8408 with opt level 9. I'm on windows, 32bit jdk 1.7.0
intel core 2 duo e7500 (2.93GHz)

I turned on TRACE_LAYOUT to be sure that new classy code in the game
D:\workspace\rhino-invokedynamic>%JAVA_HOME%/bin/java -Dorg.mozilla.javascript.classy=true -jar build\rhino1_7R3\js.jar
-opt 9 getprop.js
recognized option -Dorg.mozilla.javascript.classy=true
New Layout root: []/proto=null#26362458
New Layout: [[Mapping toString->0]]/proto=null#29644593
New Layout: [[Mapping toString->0], [Mapping toLocaleString->1]]/proto=null#17175466
New Layout: [[Mapping toString->0], [Mapping toLocaleString->1], [Mapping valueOf->2]]/proto=null#32842848
New Layout: [[Mapping toString->0], [Mapping toLocaleString->1], [Mapping valueOf->2], [Mapping hasOwnProperty->3]]/prot
o=null#19356212
New Layout: [[Mapping toString->0], [Mapping toLocaleString->1], [Mapping valueOf->2], [Mapping hasOwnProperty->3], [Map
ping propertyIsEnumerable->4]]/proto=null#9640409
New Layout: [[Mapping toString->0], [Mapping toLocaleString->1], [Mapping valueOf->2], [Mapping hasOwnProperty->3], [Map
ping propertyIsEnumerable->4], [Mapping isPrototypeOf->5]]/proto=null#25563270
New Layout: [[Mapping toString->0], [Mapping toLocaleString->1], [Mapping valueOf->2], [Mapping hasOwnProperty->3], [Map
ping propertyIsEnumerable->4], [Mapping isPrototypeOf->5], [Mapping toSource->6]]/proto=null#10936489
New Layout: [[Mapping toString->0], [Mapping toLocaleString->1], [Mapping valueOf->2], [Mapping hasOwnProperty->3], [Map
ping propertyIsEnumerable->4], [Mapping isPrototypeOf->5], [Mapping toSource->6], [Mapping defineGetter->7]]/proto=n
ull#31947169
New Layout: [[Mapping toString->0], [Mapping toLocaleString->1], [Mapping valueOf->2], [Mapping hasOwnProperty->3], [Map
ping propertyIsEnumerable->4], [Mapping isPrototypeOf->5], [Mapping toSource->6], [Mapping defineGetter->7], [Mappin
g defineSetter->8]]/proto=null#31616594
New Layout: [[Mapping toString->0], [Mapping toLocaleString->1], [Mapping valueOf->2], [Mapping hasOwnProperty->3], [Map
ping propertyIsEnumerable->4], [Mapping isPrototypeOf->5], [Mapping toSource->6], [Mapping defineGetter->7], [Mappin
g defineSetter->8], [Mapping lookupGetter->9]]/proto=null#12112029
New Layout: [[Mapping toString->0], [Mapping toLocaleString->1], [Mapping valueOf->2], [Mapping hasOwnProperty->3], [Map
ping propertyIsEnumerable->4], [Mapping isPrototypeOf->5], [Mapping toSource->6], [Mapping defineGetter->7], [Mappin
g defineSetter->8], [Mapping lookupGetter->9], [Mapping lookupSetter->10]]/proto=null#25688561
Probing cache: [[Mapping toString->0]]/proto=null#29644593
New Layout: [[Mapping log->0]]/proto=null#25377948
Probing cache: [[[Mapping toString->0]]/proto=null#29644593, [[Mapping log->0]]/proto=null#25377948]
New Layout: [[Mapping foo->0]]/proto=null#16796536
New Layout: [[Mapping foo->0], [Mapping bar->1]]/proto=null#5953458
New Layout: [[Mapping foo->0], [Mapping bar->1], [Mapping dng->2]]/proto=null#465559
New Layout: [[Mapping foo->0], [Mapping bar->1], [Mapping dng->2], [Mapping baz->3]]/proto=null#6468511
New Layout: [[Mapping foo->0], [Mapping bar->1], [Mapping dng->2], [Mapping baz->3], [Mapping mur->4]]/proto=null#334604
92
8093
8097
8199
8345
8291
8222
8337
8993
8351
8408

@hns
Copy link
Author

hns commented Aug 2, 2011

This is on a 3 year old thinkpad with current ubuntu and 32 bit os and jvm. Are you using the latest version from the invokedynamic branch? Oh, and i it's the server HotSpot VM of course (as shown in the version info)

I'd like to repeat what I said in my posting to the mailing list - this benchmark doesn't have any significance for real world code. In fact I'd even say it is slightly disappointing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment