Last active
April 5, 2021 10:26
-
-
Save wooandoo/b40e9f4f2516c0b6aa59d0c65b12515e to your computer and use it in GitHub Desktop.
error management #jsbench #jsperf (http://jsbench.github.io/#b40e9f4f2516c0b6aa59d0c65b12515e) #jsbench #jsperf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"/> | |
<title>error management #jsbench #jsperf</title> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/benchmark/1.0.0/benchmark.min.js"></script> | |
<script src="./suite.js"></script> | |
</head> | |
<body> | |
<h1>Open the console to view the results</h1> | |
<h2><code>cmd + alt + j</code> or <code>ctrl + alt + j</code></h2> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
(function (factory) { | |
if (typeof Benchmark !== "undefined") { | |
factory(Benchmark); | |
} else { | |
factory(require("benchmark")); | |
} | |
})(function (Benchmark) { | |
var suite = new Benchmark.Suite; | |
Benchmark.prototype.setup = function () { | |
let count = 0 | |
// -------------------------------------------------- | |
const execute_with_exception = (value) => { | |
if (value === 1) | |
return true | |
else | |
throw "bad value" | |
} | |
// -------------------------------------------------- | |
const execute_with_callback = (value, callback) => { | |
if (value === 1) | |
return callback(true, undefined) | |
else | |
return callback(undefined, "bad value") | |
} | |
// -------------------------------------------------- | |
const execute_and_return_error_and_result_object = (value) => { | |
if (value === 1) | |
return { result: true, error: undefined } | |
else | |
return { result: false, error: "bad value" } | |
} | |
// -------------------------------------------------- | |
const execute_and_return_error_and_result_array = (value) => { | |
if (value === 1) | |
return [ true, undefined ] | |
else | |
return [ false, "bad value" ] | |
} | |
// -------------------------------------------------- | |
const try_execute = (result, map_fn, catch_fn) => { | |
if (result.error === undefined) { | |
return map_fn(result.result) | |
} else { | |
return catch_fn(result.error) | |
} | |
} | |
// -------------------------------------------------- | |
const execute_and_return_string_type = (value) => { | |
if (value === 1) | |
return { _type: 'result', result: true } | |
else | |
return { _type: 'error', error: "bad value" } | |
} | |
// -------------------------------------------------- | |
const execute_and_return_number_type = (value) => { | |
if (value === 1) | |
return { _type: 1, result: true } | |
else | |
return { _type: -1, error: "bad value" } | |
} | |
// -------------------------------------------------- | |
const execute_with_adt = (value) => { | |
if (value === 1) | |
return Right(true) | |
else | |
return Left("bad value") | |
} | |
// -------------------------------------------------- | |
const execute_with_class_adt = (value) => { | |
if (value === 1) | |
return RightClass.of(true) | |
else | |
return LeftClass.of("bad value") | |
} | |
// -------------------------------------------------- | |
const Right = (result) => { | |
return { | |
map(fn) { | |
return Right(fn(result)) | |
}, | |
catch(fn) { | |
}, | |
value() { | |
return result | |
}, | |
error() { | |
return undefined | |
} | |
} | |
} | |
// -------------------------------------------------- | |
const Left = (error_message) => { | |
const left = { | |
map(fn) { | |
return left | |
}, | |
catch(fn) { | |
return Right(fn(error_message)) | |
}, | |
value() { | |
return undefined | |
}, | |
error() { | |
return error_message | |
} | |
} | |
return left | |
} | |
// -------------------------------------------------- | |
class RightClass { | |
static of(value) { | |
return new RightClass(value) | |
} | |
constructor(value) { | |
this.value = value | |
} | |
map(fn) { | |
return new RightClass(fn(this.value)) | |
} | |
catch(fn) { | |
return this | |
} | |
value() { | |
return this.value | |
} | |
error() { | |
return undefined | |
} | |
} | |
// -------------------------------------------------- | |
class LeftClass { | |
static of(error_message) { | |
return new LeftClass(error_message) | |
} | |
constructor(error_message) { | |
this.error_message = error_message | |
} | |
map(fn) { | |
return this | |
} | |
catch(fn) { | |
return new RightClass(fn(this.error_message)) | |
} | |
value() { | |
return undefined | |
} | |
error() { | |
return this.error_message | |
} | |
} | |
}; | |
suite.add("const { result, error } = execute_and_return_error_and_result_object(1)", function () { | |
const { result, error } = execute_and_return_error_and_result_object(1) | |
if (error === undefined) { | |
count++ | |
} else { | |
count++ | |
} | |
const ko_result = execute_and_return_error_and_result_object(2) | |
if (ko_result.error === undefined) { | |
count++ | |
} else { | |
count++ | |
} | |
}); | |
suite.add("const [ result, error ] = execute_and_return_error_and_result_array(1)", function () { | |
const [ result, error ] = execute_and_return_error_and_result_array(1) | |
if (error === undefined) { | |
count++ | |
} else { | |
count++ | |
} | |
const ko_result = execute_and_return_error_and_result_array(2) | |
if (ko_result[1] === undefined) { | |
count++ | |
} else { | |
count++ | |
} | |
}); | |
suite.add("const ok_result = execute_and_return_number_type(1)", function () { | |
const ok_result = execute_and_return_number_type(1) | |
if (ok_result._type === 1) { | |
count++ | |
} else { | |
count++ | |
} | |
const ko_result = execute_and_return_number_type(2) | |
if (ko_result._type === 1) { | |
count++ | |
} else { | |
count++ | |
} | |
}); | |
suite.add("const ok_result = execute_and_return_string_type(1)", function () { | |
const ok_result = execute_and_return_string_type(1) | |
if (ok_result._type === 'result') { | |
count++ | |
} else { | |
count++ | |
} | |
const ko_result = execute_and_return_string_type(2) | |
if (ko_result._type === 'result') { | |
count++ | |
} else { | |
count++ | |
} | |
}); | |
suite.add("execute_with_callback(1, (result, error) => {", function () { | |
execute_with_callback(1, (result, error) => { | |
if (error === undefined) { | |
count++ | |
} else { | |
count++ | |
} | |
}) | |
execute_with_callback(2, (result, error) => { | |
if (error === undefined) { | |
count++ | |
} else { | |
count++ | |
} | |
}) | |
}); | |
suite.add("execute_with_class_adt(1)", function () { | |
execute_with_class_adt(1) | |
.map(() => count++) | |
.catch(() => count++) | |
execute_with_class_adt(2) | |
.map(() => count++) | |
.catch(() => count++) | |
}); | |
suite.add("check the cost of map / catch sequence", function () { | |
// check the cost of map / catch sequence | |
execute_with_class_adt(1) | |
.map(() => count++) | |
.map(() => count++) | |
.catch(() => count++) | |
execute_with_class_adt(2) | |
.map(() => count++) | |
.catch(() => count++) | |
.catch(() => count++) | |
}); | |
suite.add("try_execute(", function () { | |
try_execute( | |
execute_and_return_error_and_result_object(1), | |
value => count++, | |
error_message => count++ | |
) | |
try_execute( | |
execute_and_return_error_and_result_object(2), | |
value => count++, | |
error_message => count++ | |
) | |
}); | |
suite.add("execute_with_adt(1)", function () { | |
execute_with_adt(1) | |
.map(() => count++) | |
.catch(() => count++) | |
execute_with_adt(2) | |
.map(() => count++) | |
.catch(() => count++) | |
}); | |
suite.add("try {", function () { | |
try { | |
execute_with_exception(1) | |
count++ | |
} catch(error) { | |
count++ | |
} | |
try { | |
execute_with_exception(2) | |
count++ | |
} catch(error) { | |
count++ | |
} | |
}); | |
suite.on("cycle", function (evt) { | |
console.log(" - " + evt.target); | |
}); | |
suite.on("complete", function (evt) { | |
console.log(new Array(30).join("-")); | |
var results = evt.currentTarget.sort(function (a, b) { | |
return b.hz - a.hz; | |
}); | |
results.forEach(function (item) { | |
console.log((idx + 1) + ". " + item); | |
}); | |
}); | |
console.log("error management #jsbench #jsperf"); | |
console.log(new Array(30).join("-")); | |
suite.run(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment