Performance Boost for Mobile Apps
If you are developing on Javascript / Kotlin / Swift, below information is equally valid. How do we write our logic to do better on performance.
There is a good video by Charles Leiserson, MIT on Youtube which explains how same algorithm to multiply 4096*4096 matrix can be written that takes 6 hours to compute to bring down to 54 seconds. Cool right ? I have attached the link to the video in this post for your reference - https://www.youtube.com/watch?v=o7h_sYMk_oc
But why this is connected to mobile application development?
For mobile apps, we have to ensure that we do all our operation in 16ms or less so our UI Thread is free. When I was going through some basic, I notice that if you rearrange your code little bit, you can do the same operation at 5x effective. I am going to demonstrate some example, but I would encourage you all to please try yourself.
Example 1 (Javascript):
Compare 2 object is same or not. We are going to call the same function 100,000 times.
for (let i = 0; i < 100000; i++) {
isEqual({ id: 1 }, { id: 2 })
}
Take 1 :
_.isEqual(object1, object2)
When we use lodash.isEqual check, this operation takes 44ms
Take 2:
if (Object.keys(object1).length !== Object.keys(object2).length)
return false
let keys = Object.keys(object1)
for (let i =0; i < keys.length; i++) {
let k = keys[i]
if (object1[k] !== object2[k]) {
if (
typeof object1[k] === 'object' &&
typeof object2[k] === 'object'
) {
if (!isObjectEquals(object1[k], object2[k])) {
return false
}
} else {
return false
}
}
}
return true
Our custom method, which will check each keys takes 39ms. This is better than Lodash. This method is called 2 times still we are better than lodash.
isObjectEquals(object1, object2) && isObjectEquals(object2, object1)
Take 3:
for (let k in object1) {
if (object1[k] !== object2[k]) {
if (
typeof object1[k] === 'object' &&
typeof object2[k] === 'object'
) {
if (!isObjectEquals(object1[k], object2[k])) {
return false
}
} else {
return false
}
}
}
return true
Checking it manually takes 4ms which is 10 times better than lodash. This is because Object.keys is costly compare to for..in loop.
Recommended by LinkedIn
isDeepEqual check is common in our code and just imagine the performance improvement you get. Not only this, I tried Array Comparison, left Join, right join etc, all reduce to less than 10ms for 100,000 operations.
Please run this repo for more examples - https://github.com/nalinchhajer2/javascript-ds-algo
Example 2 (Kotlin):
Kotlin is suppose to be better than javascript, but I was surprised by a single experiment that I performed. I am open for more helping hands who is interested to work on performance. I am working on a kotlin-ds-repo where I am trying to replicate all the experiment I performed on `javascript-ds-algo` github project here https://github.com/nalinchhajer2/kotlin-ds-algo . This is WIP and I am doing in my free time, so you will see slow update.
Experiment 2 : Delete all item in array one my one
Lets perform simple experiment, we will add 100,000 items in array and delete item one by one in a loop.
In Kotlin:
val array = mutableListOf<Int>() // 0 MS
for (i in 0 until TEST_SIZE) { // 4 ms
array.add(i)
}
for (i in array.indices) { // 484 ms
array.removeAt(0)
}
It took total 489ms to do the operation.
In Javascript :
let array = []
for (let i = 0; i < testSize; i++) { // 1ms
array.push(i)
}
let copyArray = [...array] // 1 ms
for (let i = testSize - 1; i >= 0; i--) { // 4ms
copyArray.splice(i, 1)
}
It took just 7ms to do the operation, which is 70 time better than Kotlin. The above operation was performed on same machine. For now my experiment have started, I might do same thing on swift later. Feel free to connect with me if you are interested in learning DS/Algorithm or want to have a general chat on Performance of apps.