Skip to content

Instantly share code, notes, and snippets.

@cevek
Last active January 23, 2017 20:40
Show Gist options
  • Save cevek/6e1ad447cb23065d688270d8ce1b88e2 to your computer and use it in GitHub Desktop.
Save cevek/6e1ad447cb23065d688270d8ce1b88e2 to your computer and use it in GitHub Desktop.

https://github.com/cevek/jscost

conditionals

if (k) v = 1: 0.8ns if (k) v = 1 else v = 0: 0.4ns switch 10 cases (per case): 0.4ns n1 && n2 && ... (per item): 1.7ns n1 || n2 || ... (per item): 1.5ns k == 0 ? 1 : k == 1 ? 2 : ... (10) (per cond): 0.5ns k ? 1 : 0: 0.8ns

readKey

obj.a: 0.4ns obj.a generic: 9ns obj.x(non exists) generic: 9ns obj.a mutated: 9ns obj[prop]: 18ns obj[prop] generic: 18ns obj[prop] hashtable: 10ns obj[non exist prop]: 93ns obj[non exist prop] hashtable: 86ns

functions

inline function call: 0.4ns function call: 3.7ns fn.call(obj, val): 6ns fn.apply(obj, args): 15ns non optimized function call: 27ns

create objects

{}: 9ns {a,b,c,d,e}: 9ns {a,b,c,d,e,f,g,h,i,j}: 272ns new A(){a,b,c,d,e}: 10ns new A(){a,b,c,d,e,f,g,h,k,l}: 18ns

create objects 10 props (per key)

obj.key=1: 4.5ns obj[strProp]=1: 18ns obj[numProp]=1: 29ns obj[strProp|numProp]=1: 32ns obj = new A(); obj[prop]=1: 33ns obj[prop]=1 hashtable: 27ns

read objectKeysValues (per key)

object.keys({a,b,c,d,e}): 17ns object.keys + values({a,b,c,d,e}): 42ns for in only keys {a,b,c,d,e}: 2.7ns for in generic only keys {a,b,c,d,e}: 2.7ns for in generic {a,b,c,d,e}: 4ns for in generic(with hashtables) {a,b,c,d,e}: 28ns

objectKeys in hashtable (per key)

object.keys hashtable + values({a,b,c,d,e}): 82ns for in hashtables {a,b,c,d,e}: 68ns for in hashtables generic {a,b,c,d,e}: 77ns

readArray

arr[1]: 0.5ns arr[i]: 1ns arr[i] generic value: 1ns uint8[i]: 1ns uint16[i]: 1ns

createArray

empty array: 5ns [0-9] const number: 5ns [0-9] var number: 91ns [0-9] const primitives(bool, number, string, null, not undefined): 5ns [0-8] mixed: 14ns [0-9] mixed: 95ns [0-9] const number + undefined: 88ns [] grow 0->9, mixed: 45ns [0-9] primitives + set 5 vars, mixed: 24ns new Array(10), empty: 14ns new Array(10), mixed: 22ns new Uint8Array(10), empty: 83ns new Uint32Array(10), empty: 80ns
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment