Skip to content

Instantly share code, notes, and snippets.

@shuhei
Created October 16, 2018 06:32
Show Gist options
  • Save shuhei/edf5f7903d677e42c1f118b81c3b9867 to your computer and use it in GitHub Desktop.
Save shuhei/edf5f7903d677e42c1f118b81c3b9867 to your computer and use it in GitHub Desktop.
A test for optimization of optional properties
const a = {
a: 1,
b: null
};
const b = {
a: 2,
b: null
};
const c = {
a: 2,
b: 123
};
const d = {
a: 2
};
const e = {
a: 2,
b: undefined
};
const f = {
a: 2,
b: undefined
};
const g = {
a: 2,
b: {
c: 1,
d: 2
}
};
const h = {
a: 2,
b: 'hello'
};
console.log('null vs null:', %HaveSameMap(a, b));
console.log('null vs 123:', %HaveSameMap(a, c));
console.log('null vs no property:', %HaveSameMap(a, d));
console.log('null vs undefined:', %HaveSameMap(a, e));
console.log('123 vs no property:', %HaveSameMap(c, d));
console.log('123 vs undefined:', %HaveSameMap(c, e));
console.log('no property vs undefined:', %HaveSameMap(d, e));
console.log('undefined vs undefined:', %HaveSameMap(e, f));
console.log('number vs object:', %HaveSameMap(c, g));
console.log('undefined vs object:', %HaveSameMap(e, g));
console.log('undefined vs string:', %HaveSameMap(f, h));
%DebugPrint(a);
%DebugPrint(c);
%DebugPrint(d);
%DebugPrint(e);
let sum = 0;
function getA(obj) {
if (obj.b) {
sum += obj.b;
}
}
let counter = 100000;
while (counter-- > 0) {
getA(c);
getA(d);
}
console.log(sum);
%DebugPrint(getA);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment