oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.56k stars 2.72k forks source link

console.log shows inherited prototype properties #12365

Open PhilBladen opened 3 months ago

PhilBladen commented 3 months ago

What version of Bun is running?

1.1.18+5a0b93523

What platform is your computer?

Microsoft Windows NT 10.0.22631.0 x64

What steps can reproduce the bug?

Minimal example:

class Base {}
Base.prototype.visibleBaseProp = 'base value';

class Derived extends Base {
    constructor() {
        super();
        this.visibleDerivedProp = 'derived value';
    }
}

console.log(new Derived());

What is the expected behavior?

NodeJS outputs the expected result of:

Derived { visibleDerivedProp: 'derived value' }

What do you see instead?

Bun outputs the unexpected result showing the inherited prototype property:

Derived {
  visibleDerivedProp: "derived value",
  visibleBaseProp: "base value",
}

Additional information

No response

PhilBladen commented 3 months ago

This can cause printing objects that inherit complex classes to dump enormous amounts of text into the console. In my case, logging a MariaDB query result, BunJS logged >4GB of text in a single console.log statement, whereas NodeJS logged only ~150 bytes for the same object!!!

If this is an intentional feature, I suggest there be a way to control it. Currently it feels like a bug.

PhilBladen commented 3 months ago

Here's an even better example of where it becomes an issue:

class MyClass {}
for (let i = 0; i < 100; i++) MyClass.prototype[i] = new MyClass();
console.log(new MyClass());

Bun is printing all prototype properties for every instance of itself semi-recursively (until it detects a circular reference).

To compare Bun and Node in this example