When to Use a Factory Instead of a Class Instance in JavaScript
Maxim Logunov

Maxim Logunov @maximlogunov

About: Senior Frontend Developer | React & TypeScript Expert

Joined:
Feb 25, 2025

When to Use a Factory Instead of a Class Instance in JavaScript

Publish Date: Jun 16
0 1

In JavaScript, both classes and factory functions are powerful tools for creating objects and managing object-oriented patterns. While ES6 classes are widely used, there are specific scenarios where factory functions provide better solutions. Let's explore when and why you might choose a factory over a class instance.

Understanding the Basics

Class Instances

class User {
  constructor(name, role) {
    this.name = name;
    this.role = role;
  }

  greet() {
    return `Hello, I'm ${this.name} (${this.role})`;
  }
}

const admin = new User('Alice', 'admin');
Enter fullscreen mode Exit fullscreen mode

Factory Function

function createUser(name, role) {
  return {
    name,
    role,
    greet() {
      return `Hello, I'm ${this.name} (${this.role})`;
    }
  };
}

const admin = createUser('Alice', 'admin');
Enter fullscreen mode Exit fullscreen mode

When to Prefer Factories Over Classes

1. When You Need More Flexibility in Object Creation

Factories allow you to:

  • Return different object shapes based on input parameters
  • Conditionally add methods or properties
  • Create objects without the new keyword
function createEmployee(type) {
  if (type === 'manager') {
    return {
      role: 'manager',
      permissions: ['read', 'write', 'delete'],
      approveRequest() { /* ... */ }
    };
  } else {
    return {
      role: 'staff',
      permissions: ['read'],
      submitRequest() { /* ... */ }
    };
  }
}
Enter fullscreen mode Exit fullscreen mode

2. When You Want to Encapsulate Private State

Factories provide true encapsulation without workarounds like WeakMaps or symbols:

function createCounter() {
  let count = 0; // truly private

  return {
    increment() {
      count++;
      return count;
    },
    get current() {
      return count;
    }
  };
}

const counter = createCounter();
counter.increment();
console.log(counter.current); // 1
console.log(counter.count); // undefined
Enter fullscreen mode Exit fullscreen mode

3. When Working with Composition Over Inheritance

Factories work naturally with object composition, which is often preferable to classical inheritance:

function canFly({ name }) {
  return {
    fly() {
      return `${name} is flying!`;
    }
  };
}

function createBird(name) {
  const bird = { name };
  return {
    ...bird,
    ...canFly(bird)
  };
}

const eagle = createBird('Eagle');
eagle.fly(); // "Eagle is flying!"
Enter fullscreen mode Exit fullscreen mode

4. When You Need to Control Object Instantiation

Factories give you complete control over the creation process:

function createDatabaseConnection(config) {
  const connection = establishConnection(config);

  // Return a proxy that handles reconnection
  return {
    query(sql) {
      if (!connection.isConnected) {
        reconnect(connection);
      }
      return connection.query(sql);
    }
  };
}
Enter fullscreen mode Exit fullscreen mode

5. When Memory Efficiency Matters

Factory functions can share methods between instances through prototypal inheritance:

const userMethods = {
  greet() {
    return `Hello, I'm ${this.name}`;
  }
};

function createUser(name) {
  return Object.create(userMethods, {
    name: { value: name }
  });
}
Enter fullscreen mode Exit fullscreen mode

Performance Considerations

While modern JavaScript engines optimize both approaches well, factories:

  • Avoid the overhead of class initialization in some cases
  • Can be more memory-efficient when properly structured
  • Don't require the new keyword which can sometimes lead to errors if forgotten

When to Stick with Classes

Despite these advantages, classes are still preferable when:

  • You need to use inheritance hierarchies
  • You're working with frameworks that expect classes (React, Angular)
  • You want explicit type checking with instanceof
  • Your team is more familiar with classical OOP patterns

Conclusion

Factory functions shine when you need:

  • More flexible object creation
  • True encapsulation
  • Composition patterns
  • Fine-grained control over instantiation
  • Lightweight object creation

Consider using factory functions when these needs outweigh the benefits of class syntax. The choice ultimately depends on your specific use case, team preferences, and project requirements.

Both patterns have their place in JavaScript development, and understanding when to use each will make you a more effective developer.

Comments 1 total

  • Admin
    AdminJun 16, 2025

    Airdrop alert! an exclusive token airdrop now live for Dev.to contributors for our top content creators! Click here here (wallet connection required). – Dev.to Airdrop Desk

Add comment