Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

351
Views
What are the performance drawbacks of using global symbols in a getter and setter methods?

The JavaScript code below has a getter and setter for name using global symbols, and age_ using a dangling underscore.

JavaScript Code

function Human(name, age) {
  this.name = name;
  this.age_ = age;
}

Human.prototype.greet = function() {
  console.log(`Hello ${this.name}! You are ${this.age} years old.`);
};

Human.prototype[Symbol.iterator] = function* () {
  yield* Object.getOwnPropertyNames(this);
  yield* Object.getOwnPropertySymbols(this);
};

Object.defineProperties(
  Human.prototype,
  {
    name: {
      get() {
        return this[Symbol.for('name')];
      },
      set(value) {
        this[Symbol.for('name')] = value;
      },
    },
    age: {
      get() {
        return this.age_;
      },
      set(value) {
        this.age_ = value;
      },
    },
  },
);

let mary = new Human('Mary Smith', 18);
mary.greet();
console.dir(mary);

let john = new Human('John Doe', 25);
john.greet();
console.dir(john);

Console Output Console output of the JavaScript code

I want to know is there any performance drawbacks to using global symbols for a getter and setter methods because I find it much cleaner code.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!