Skip to content

Safely ignore error stack trace #130

@H4ad

Description

@H4ad

I saw this library https://github.com/isaacs/catcher and I decided to wrote a benchmark:

Doing some changes on bench/error.js, the result was:

name ops/sec samples
Error 337,720 64
Error (stackTraceLimit=0) 3,284,296 94
NodeError 283,026 99
NodeError (stackTraceLimit=0) 3,280,933 95
NodeError Range 214,825 92
NodeError Range (stackTraceLimit=0) 3,313,990 99
Code
const { createBenchmarkSuite } = require('../common')

const suite = createBenchmarkSuite('Node.js Error')

suite
  .add('Error', function () {
    try {
      new Error('test')
    } catch (e) { }
  })
  .add('Error (stackTraceLimit=0)', function () {
    const originalStackTraceLimit = Error.stackTraceLimit
    Error.stackTraceLimit = 0
    try {
      new Error('test')
    } catch (e) { }
    finally {
      Error.stackTraceLimit = originalStackTraceLimit;
    }
  })
  .add('NodeError', function () {
    try {
      new TypeError('test')
    } catch (e) { }
  })
  .add('NodeError (stackTraceLimit=0)', function () {
    const originalStackTraceLimit = Error.stackTraceLimit
    Error.stackTraceLimit = 0
    try {
      new TypeError('test')
    } catch (e) { }
    finally {
      Error.stackTraceLimit = originalStackTraceLimit;
    }
  })
  .add('NodeError Range', function () {
    try {
      new RangeError('test')
    } catch (e) { }
  })
  .add('NodeError Range (stackTraceLimit=0)', function () {
    const originalStackTraceLimit = Error.stackTraceLimit
    Error.stackTraceLimit = 0
    try {
      new RangeError('test')
    } catch (e) { }
    finally {
      Error.stackTraceLimit = originalStackTraceLimit;
    }
  })
  .run({ async: false })

Based on this assumption, maybe we can find places on Node where we can safely ignore the stackTraceLimit, using this search, I found some places:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions