IfElif Performance Test

node v18.11.0
version: 1.0.0
endpointsharetweet
const { _if } = require('ifelif') const { If: Iffy } = require("@pennah/iffy"); const If = require('ifx'); const Jif = require('jif'); const testCount = 1000000; const a = 3; function normal() { const start = performance.now(); for (let i = 0; i < testCount; i++) { let result if (a === 1) { result = 'a is 1' } else if (a === 2) { result = 'a is 2' } else if (a === 3) { result = 'a is 3' } else { result = 'a is not 1, 2, or 3' } if (result !== 'a is 3') { throw new Error('result should be "a is 3"') } } console.log(`normal: ${performance.now() - start} ms`) } function ternary() { const start = performance.now(); for (let i = 0; i < testCount; i++) { const result = a === 1 ? 'a is 1' : a === 2 ? 'a is 2' : a === 3 ? 'a is 3' : 'a is not 1, 2, or 3' if (result !== 'a is 3') { throw new Error('result should be "a is 3"') } } console.log(`ternary: ${performance.now() - start} ms`) } function ifelif() { const start = performance.now(); for (let i = 0; i < testCount; i++) { const result = _if(a === 1).then('a is 1') .elif(a === 2).then('a is 2') .elif(a === 3).then('a is 3') .else('a is not 1, 2, or 3') if (result !== 'a is 3') { throw new Error('result should be "a is 3"') } } console.log(`ifelif: ${performance.now() - start} ms`) } function iffy() { const start = performance.now(); for (let i = 0; i < testCount; i++) { const result = Iffy(a === 1, { then: 'a is 1', else: Iffy(a === 2, { then: 'a is 2', else: Iffy(a === 3, { then: 'a is 3', else: 'a is not 1, 2, or 3' }) }) }) if (result !== 'a is 3') { console.log(result) throw new Error('result should be "a is 3"') } } console.log(`iffy: ${performance.now() - start} ms`) } function ifx() { const start = performance.now(); for (let i = 0; i < testCount; i++) { const result = If(a === 1)(() => 'a is 1') .ElseIf(a === 2)(() => 'a is 2') .ElseIf(a === 3)(() => 'a is 3') .Else(() => 'a is not 1, 2, or 3') if (result !== 'a is 3') { throw new Error('result should be "a is 3"') } } console.log(`ifx: ${performance.now() - start} ms`) } function jif() { const start = performance.now(); for (let i = 0; i < testCount; i++) { const result = Jif(a === 1, 'a is 1', Jif(a === 2, 'a is 2', Jif(a === 3, 'a is 3', 'a is not 1, 2, or 3' ) ) ) if (result !== 'a is 3') { throw new Error('result should be "a is 3"') } } console.log(`jif: ${performance.now() - start} ms`) } normal() ternary() ifelif() iffy() ifx() jif()
Created from: https://npm.runkit.com/ifelif
Loading…

no comments

    sign in to comment