Ever true switch vs Else if

Benchmark created on


Description

Test ever true switch and else if

Setup

const doSomething = () => {
	const temp = 1 + 2 + 3
	return temp
}

const a = 500
const b = 'abc'
const c = {
	a: 1,
	b: 2,
	c: 3
}
const d = {
	a: {
		aa: 'abc',
		ab: {
			aba: 'abcdefghijk',
			abb: 'abcdefghijk',
			abc: 'abcdefghijk',
			abd: 'abcdefghijk',
			abe: 'abcdefghijk'
		}
	},
	b: {
		ba: 'def',
		bb: 'abcdefghijkabcdefghijk'
	}
}
const e = 'abc'

Test runner

Ready to run.

Testing in
TestOps/sec
Ever true switch
switch (true) {
	case (a === 500 && b !== 'abc'): {
		doSomething()
		break
	}
	case (c['a'] === 1): {
		doSomething()
		break
	}
	case (c.b === 'b' && d.b.ba === 'def'): {
		doSomething()
		break
	}
	case (d.b.bb): {
		doSomething()
		break
	}
	case (e in d.a.ab): {
		doSomething()
		break
	}
	default: {
		break
	}
}

switch (true) {
	case (a === 700 && b !== 'abc'): {
		doSomething()
		break
	}
	case (c['a'] === 1): {
		doSomething()
		break
	}
	case (c.b === 'b' && d.b.ba === 'def'): {
		doSomething()
		break
	}
	case (d.b.bb): {
		doSomething()
		break
	}
	case (e in d.a.ab): {
		doSomething()
		break
	}
	default: {
		break
	}
}


switch (true) {
	case (a === 700 && b !== 'abc'): {
		doSomething()
		break
	}
	case (c['a'] === 2): {
		doSomething()
		break
	}
	case (c.b === 'b' && d.b.ba === 'def'): {
		doSomething()
		break
	}
	case (d.b.bb): {
		doSomething()
		break
	}
	case (e in d.a.ab): {
		doSomething()
		break
	}
	default: {
		break
	}
}


switch (true) {
	case (a === 700 && b !== 'abc'): {
		doSomething()
		break
	}
	case (c['a'] === 2): {
		doSomething()
		break
	}
	case (c.b === 'c' && d.b.ba === 'ghi'): {
		doSomething()
		break
	}
	case (d.b.bb): {
		doSomething()
		break
	}
	case (e in d.a.ab): {
		doSomething()
		break
	}
	default: {
		break
	}
}

switch (true) {
	case (a === 700 && b !== 'abc'): {
		doSomething()
		break
	}
	case (c['a'] === 2): {
		doSomething()
		break
	}
	case (c.b === 'c' && d.b.ba === 'ghi'): {
		doSomething()
		break
	}
	case (!d.b.bb): {
		doSomething()
		break
	}
	case (e in d.a.ab): {
		doSomething()
		break
	}
	default: {
		break
	}
}
ready
Else if
if (a === 500 && b !== 'abc') {
    doSomething()
}
else if (c['a'] === 1) {
    doSomething()
}
else if (c.b === 'b' && d.b.ba === 'def') {
    doSomething()
}
else if (d.b.bb) {
    doSomething()
}
else if (e in d.a.ab) {
    doSomething()
}

if (a === 700 && b !== 'abc') {
    doSomething()
}
else if (c['a'] === 1) {
    doSomething()
}
else if (c.b === 'b' && d.b.ba === 'def') {
    doSomething()
}
else if (d.b.bb) {
    doSomething()
}
else if (e in d.a.ab) {
    doSomething()
}

if (a === 700 && b !== 'abc') {
    doSomething()
}
else if (c['a'] === 2) {
    doSomething()
}
else if (c.b === 'b' && d.b.ba === 'def') {
    doSomething()
}
else if (d.b.bb) {
    doSomething()
}
else if (e in d.a.ab) {
    doSomething()
}

if (a === 700 && b !== 'abc') {
    doSomething()
}
else if (c['a'] === 2) {
    doSomething()
}
else if (c.b === 'c' && d.b.ba === 'def') {
    doSomething()
}
else if (d.b.bb) {
    doSomething()
}
else if (e in d.a.ab) {
    doSomething()
}

if (a === 700 && b !== 'abc') {
    doSomething()
}
else if (c['a'] === 2) {
    doSomething()
}
else if (c.b === 'c' && d.b.ba === 'def') {
    doSomething()
}
else if (d.b.bb) {
    doSomething()
}
else if (e in d.a.ab) {
    doSomething()
}

if (a === 700 && b !== 'abc') {
    doSomething()
}
else if (c['a'] === 2) {
    doSomething()
}
else if (c.b === 'c' && d.b.ba === 'def') {
    doSomething()
}
else if (!d.b.bb) {
    doSomething()
}
else if (e in d.a.ab) {
    doSomething()
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.