Skip to content

Instantly share code, notes, and snippets.

@wochap
Last active October 18, 2016 15:07
Show Gist options
  • Save wochap/c3bd19ea103f601ffd21 to your computer and use it in GitHub Desktop.
Save wochap/c3bd19ea103f601ffd21 to your computer and use it in GitHub Desktop.
javascript helpers
/* how to iterate a object */
post = {title: 'New features in JS', replies: 19}
post[Symbol.iterator] = function *() {
let properties = Object.keys(this)
for (let p of properties) {
yield this[p]
}
}
for (let p of post) {
console.log(p)
}
/* is this value a number */
function isThisActuallyNumberDontLie (data) {
return (typeof data === 'number' && !isNaN(data))
}
const position = this.getBoundingClientRect()
const midX = position.left + position.width * 0.5
const midY = position.top + position.height * 0.5

const rX = Math.max(midX, window.innerWidth - midX)
const rY = Math.max(midX, window.innerHeght - midY)

const radius = Math.sqrt(rX * rX + rY * rY)

domElm.style.width = `${radius * 2}px`
domElm.style.height = `${radius * 2}px`
/* test functions speed */
class SpeedTest {
constructor (testImplement, testParams, repetitions = 10000) {
this.testImplement = testImplement
this.testParams = testParams
this.repetitions = repetitions
this.average = 0
}
speedTest () {
if (this.testImplement(this.testParams) === false) {
alert('Test failed with those parameters.')
return
}
let beginTime, endTime, sumTimes = 0
for (let i = 0, x = this.repetitions; i < x; i++) {
beginTime = +new Date()
this.testImprement(this.testParams)
endTime = +new Date()
sumTimes += endTime - beginTime
}
this.average = sumTimes / this.repetitions
return console.log(`Average execution across ${this.repetitions}: ${this.average}`)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment