jQuery addClass example
Change class name on click in jQuery
by rakesh makluri
HTML
<div id="banner-message">
<p>Hello World</p>
<button>Change color</button>
</div>
CSS
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#banner-message {
background: #fff;
border-radius: 4px;
padding: 20px;
font-size: 25px;
text-align: center;
transition: all 0.2s;
margin: 0 auto;
width: 300px;
}
button {
background: #0084ff;
border: none;
border-radius: 5px;
padding: 8px 14px;
font-size: 15px;
color: #fff;
}
#banner-message.alt {
background: #0084ff;
color: #fff;
margin-top: 40px;
width: 200px;
}
#banner-message.alt button {
background: #fff;
color: #000;
}
JavaScript
class MyArray {
constructor(initialArray) {
this.length = initialArray.length
this.data = initialArray
}
get(index) {
return this.data[index]
}
push(item) {
this.data[this.length] = item
this.length++
return this.data
}
pop() {
const lastItem = this.data[this.length - 1]
delete this.data[this.length - 1]
this.length--
return lastItem
}
}
const TESTS = 100000 // 100k
// Custom
let myCustomArray = new MyArray([])
console.time('customClassPush');
for (let i = 0; i < TESTS; i++) {
myCustomArray.push(i)
}
console.timeEnd('customClassPush');
console.time('customClassGet');
for (let i = 0; i < TESTS; i++) {
myCustomArray.get(i)
}
console.timeEnd('customClassGet');
console.time('customClassPop');
for (let i = 0; i < TESTS; i++) {
myCustomArray.pop()
}
console.timeEnd('customClassPop');
// Native
let myNativeArray = []
console.time('nativeArrayPush');
for (let i = 0; i < TESTS; i++) {
myNativeArray.push(i)
}
console.timeEnd('nativeArrayPush');
console.time('nativeArrayGet');
for (let i = 0; i < TESTS; i++) {
myNativeArray[i]
}
console.timeEnd('nativeArrayGet');
console.time('nativeArrayPop');
for (let i = 0; i < TESTS; i++) {
myNativeArray.pop()
}
console.timeEnd('nativeArrayPop');