Skip to content

Instantly share code, notes, and snippets.

@CrazyPython
Created May 26, 2019 18:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CrazyPython/6915ab5750644995c675a716010074d7 to your computer and use it in GitHub Desktop.
Save CrazyPython/6915ab5750644995c675a716010074d7 to your computer and use it in GitHub Desktop.
time node --trace-opt --trace-deopt -e '
function bench() {
const loops = 3000;
const objSize = 4 * 3
const i32StartOffset = loops * objSize
const i32Size = 4 * 2
const ab = new ArrayBuffer(i32StartOffset + i32Size)
const f32 = new Float32Array(ab, 0)
const i32 = new Int32Array(ab, i32StartOffset)
const arr = []
class Obj {
constructor(i) {
this.i = i
}
get thing() {
return f32[ab * 3];
}
set thing(v) {
f32[ab * 3] = v
}
get thing2() {
return f32[ab * 3 + 1];
}
set thing2(v) {
f32[ab * 3 + 1] = v
}
get thing3() {
return f32[ab * 3 + 2];
}
set thing3(v) {
f32[ab * 3 + 2] = v
}
method() {
return this.thing3 > 0
}
method2() {
return this.thing - this.thing2
}
}
arr.length = loops
for (let i = 0; i < loops; i++) {
arr[i] = new Obj(i)
}
// set test
for (let i = 0; i < loops; i++) {
const obj = arr[i]
obj.thing = Math.ceil(Math.random() * 100) - 50
obj.thing3 = Math.ceil(Math.random() * 100) - 50
if (obj.method()) {
obj.thing2 = Math.ceil(Math.random() * 4)
}
}
// get test
for (let i = 0; i < loops; i++) {
const obj = arr[i]
i32[0] += Math.SQRT2 * obj.thing * obj.thing2
if ((obj.method2() - obj.method() > 0))
i32[1] += 4
};
}
console.time("A warmup")
for (let j = 0; j < 50; ++j)
bench()
console.timeEnd("A warmup")
console.time("A")
for (let j = 0; j < 500; ++j)
bench()
console.timeEnd("A")
' & time node --trace-opt --trace-deopt -e '
function bench() {
const loops = 3000;
const objSize = 4 * 3
const i32StartOffset = loops * objSize
const i32Size = 4 * 2
const ab = new ArrayBuffer(i32StartOffset + i32Size)
const f32 = new Float32Array(ab, 0)
const i32 = new Int32Array(ab, i32StartOffset)
const arr = []
class Obj {
constructor(i) {
this.i = i
}
getThing() {
return f32[ab * 3];
}
setThing(v) {
f32[ab * 3] = v
}
getThing2() {
return f32[ab * 3 + 1];
}
setThing2(v) {
f32[ab * 3 + 1] = v
}
getThing3() {
return f32[ab * 3 + 2];
}
setThing3(v) {
f32[ab * 3 + 2] = v
}
method() {
return this.getThing3() > 0
}
method2() {
return this.getThing() - this.getThing2()
}
}
arr.length = loops
for (let i = 0; i < loops; i++) {
arr[i] = new Obj(i)
}
// set test
for (let i = 0; i < loops; i++) {
const obj = arr[i]
obj.setThing(Math.ceil(Math.random() * 100) - 50)
obj.setThing3(Math.ceil(Math.random() * 100) - 50)
if (obj.method()) {
obj.setThing2(Math.ceil(Math.random() * 4))
}
}
// get test
for (let i = 0; i < loops; i++) {
const obj = arr[i]
i32[0] += Math.SQRT2 * obj.getThing() * obj.getThing2()
if ((obj.method2() - obj.method() > 0))
i32[1] += 4
};
}
console.time("B warmup")
for (let j = 0; j < 50; ++j)
bench()
console.timeEnd("B warmup")
console.time("B")
for (let j = 0; j < 500; ++j)
bench()
console.timeEnd("B")'& time node --trace-opt --trace-deopt -e '
function bench() {
const loops = 3000;
const objSize = 4 * 3
const i32StartOffset = loops * objSize
const i32Size = 4 * 2
const ab = new ArrayBuffer(i32StartOffset + i32Size)
const f32 = new Float32Array(ab, 0)
const i32 = new Int32Array(ab, i32StartOffset)
class Obj {
constructor(i) {
this.i = i
}
get thing() {
return f32[ab * 3];
}
set thing(v) {
f32[ab * 3] = v
}
get thing2() {
return f32[ab * 3 + 1];
}
set thing2(v) {
f32[ab * 3 + 1] = v
}
get thing3() {
return f32[ab * 3 + 2];
}
set thing3(v) {
f32[ab * 3 + 2] = v
}
method() {
return this.thing3 > 0
}
method2() {
return this.thing - this.thing2
}
}
// set test
for (let i = 0; i < loops; i++) {
new Obj(i).thing = Math.ceil(Math.random() * 100) - 50
new Obj(i).thing3 = Math.ceil(Math.random() * 100) - 50
if (new Obj(i).method()) {
new Obj(i).thing2 = Math.ceil(Math.random() * 4)
}
}
// get test
for (let i = 0; i < loops; i++) {
i32[0] += Math.SQRT2 * new Obj(i).thing * new Obj(i).thing2
if ((new Obj(i).method2() - new Obj(i).method() > 0))
i32[1] += 4
};
}
console.time("C warmup")
for (let j = 0; j < 50; ++j)
bench()
console.timeEnd("C warmup")
console.time("C")
for (let j = 0; j < 500; ++j)
bench()
console.timeEnd("C")'&
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment