Skip to content

Instantly share code, notes, and snippets.

@followdarko
Last active November 15, 2018 10:16
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 followdarko/56befdcf216ad51546384b4ad1181b46 to your computer and use it in GitHub Desktop.
Save followdarko/56befdcf216ad51546384b4ad1181b46 to your computer and use it in GitHub Desktop.
// https://browser.primatelabs.com/ios-benchmarks
// https://mydevice.io/devices/
// http://vizdevices.yesviz.com/devices.php
const IPHONE5_SIZE = 568
const IPAD_SIZE = 1024
const MIN_OPS = 165 /* Upper limit cpu for iPad4 */
const CACHE_KEY = 'anim'
function getCache(params) {
const ca = document.cookie.split(';')
let cacheVal = null
for (let i = 0; i < ca.length; i++) {
const c = ca[i].trim()
if (c.indexOf(CACHE_KEY) === 0) {
cacheVal = c.substring(CACHE_KEY.length + 1, c.length)
break
}
}
if (!cacheVal) {
return null
}
const cache = JSON.parse(decodeURIComponent(cacheVal))
const vals = cache.split('&').map(el => +el.slice(2))
if (vals[1] !== params.width || vals[2] !== params.height) {
return null
}
return cache
}
function setCache(isAnimate, data) {
const d = new Date()
d.setTime(d.getTime() + (1 * 24 * 60 * 60 * 1000))
const expires = `expires=${d.toGMTString()}`
const str = `a=${isAnimate}&w=${data.width}&h=${data.height}`
document.cookie = CACHE_KEY + '=' + encodeURIComponent(JSON.stringify(str)) + ';path = /;' + expires
}
function isDevicePossiblySlow(params) {
const { width, height, device } = params
if (device.type === 'desktop') return false
if (getCache(params)) return false
if (device.os === 'iOS') {
const size = width > height ? width : height
if (device.model === 'iPhone' && size > IPHONE5_SIZE) {
return false
} else if (device.model === 'iPad' && size > IPAD_SIZE) {
return false
}
}
return true
}
async function getOps() {
const startTime = new Date()
let runs = 0
let totalTime
do {
runs++
totalTime = new Date() - startTime
Math.sqrt(runs * Math.random())
} while (totalTime < 500)
const result = runs / totalTime
return result
}
export async function isAnimationEnable(params) {
if (isDevicePossiblySlow(params)) {
const ops = await getOps()
const disableAnimate = ops < MIN_OPS ? 1 : 0
makeGDocRequest(params, ops)
setCache(disableAnimate, params)
if (disableAnimate) {
return false
}
}
return true
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment