custom Error/Object, extends vs prototype vs class vs Object.create (v3)

Revision 3 of this benchmark created on


Preparation HTML

<script>
const message = 'Error message';
const category = 'Error category';
const level = 'info';
const stack = 'erro stack, erro stack, erro stack';
const customData = {
    tag1: 'tag1',
    tag2: 'tag2',
};
class ExtendsError extends Error {
    constructor(message, category, stack, customData, level) {
        super(message); // 调用父类的构造函数
        this.category = category;
        this.stack = stack;
        this.customData = customData;
        this.level = level;
    }
}
function FuncPrototype(message, category, stack, customData, level) {
    this.message = message;
    this.category = category;
    this.stack = stack;
    this.customData = customData;
    this.level = level;
}
FuncPrototype.prototype = Error.prototype;
class NormalClass {
    constructor(message, category, stack, customData, level) {
        this.message = message;
        this.category = category;
        this.stack = stack;
        this.customData = customData;
        this.level = level;
    }
}
function FuncClass(message, category, stack, customData, level) {
    this.message = message;
    this.category = category;
    this.stack = stack;
    this.customData = customData;
    this.level = level;
}
function assert(err, iof) {
    if (iof == null && !(err instanceof Error)) {
        throw 'not instanceof Error';
    }
    if (err.message !== message) {
        throw 'err.message asset failed';
    }
    if (err.stack !== stack) {
        throw 'err.stack asset failed';
    }
    if (err.category !== category) {
        throw 'err.category asset failed';
    }
    if (err.customData.tag1 !== customData.tag1 || err.customData.tag2 !== customData.tag2) {
        throw 'err.customData asset failed';
    }
    if (err.level !== level) {
        throw 'err.level asset failed';
    }
}
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
extends Error
const test1 = new ExtendsError(message, category, stack, customData, level);
assert(test1);
ready
function prototype
const test2 = new FuncPrototype(message, category, stack, customData, level);
assert(test2);
ready
Object.create
const test3 = Object.create(Error.prototype, {
    level: { value: level },
    category: { value: category },
    message: { value: message },
    stack: { value: stack },
    customData: { value: customData },
});
assert(test3);
ready
normal class
const test4 = new NormalClass(message, category, stack, customData, level);
assert(test4, 'no instanceof');
ready
function class
const test5 = new FuncClass(message, category, stack, customData, level);
assert(test5, 'no instanceof');
ready
Object.create object
const test6 = Object.create({}, {
    level: { value: level },
    category: { value: category },
    message: { value: message },
    stack: { value: stack },
    customData: { value: customData },
});
assert(test6, 'no instanceof');
ready

Revisions

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