Open In App

Methods vs Computed in Vue

Vue.js, with its simplicity and flexibility, has become a popular choice for building dynamic web applications. Among its many features, Vue provides two main ways to perform data manipulation and calculations within components: methods and computed properties. While they might seem similar at first glance, understanding the differences between them is crucial for effectively managing your Vue applications.

What are Methods?

Methods in Vue.js are simple JavaScript functions defined within the methods object of a Vue component. They are called directly from the template or within other methods using this keyword. Methods are suitable for performing actions, such as event handling, form submissions, or any other imperative logic.

Syntax:

export default {
data() {
return {
// data properties
};
},
methods: {
methodName() {
// method logic
}
}
}

Example: The below code implements Vue methods to create a counter.

<!-- App.vue file -->

<template>
    <div>
        <p>Value: {{ value }}</p>
        <button @click="increment">
            Increment
        </button>
        <p>
            Sum (Value+5): 
            {{ calculateSum() }}
        </p>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                value: 10
            };
        },
        methods: {
            increment() {
                this.value++;
            },
            calculateSum() {
                return this.value + 5;
            }
        }
    }
</script>

Output:

Screenshot-2024-04-12-064040

Benefits of Using Methods

What are Computed Properties?

Computed properties in Vue.js are functions that are defined within the computed object of a Vue component. They are reactive and automatically re-evaluate whenever their dependent data properties change. Computed properties are suitable for deriving and caching computed values based on reactive data.

Syntax:

export default {
data() {
return {
// data properties
};
},
computed: {
propertyName() {
// computed property logic
}
}
}

Example: The below code will explain the practical implementation of the computed properties in Vue.js.

<template>
    <div>
        <p>
            Counter: {{ counter }}
        </p>
        <p>
            Double Counter: 
            {{ doubleCounter }}
        </p>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                counter: 2
            };
        },
        computed: {
            doubleCounter() {
                return this.counter * 2;
            }
        }
    }
</script>

Output:

Screenshot-2024-04-12-191235

Benefits of Using Computed Properties

Use cases of each one of them

Methods:

Computed Properties:

Which is Better to Use: Methods or Computed Properties and Why?

The choice between methods and computed properties depends on the specific use case.

Differences between Methods and Computed Properties

Aspect

Methods

Computed Properties

Invocation

Called explicitly from the template or within other methods.

Automatically re-evaluated based on reactive dependencies.

Reactive

Not reactive; execute when called.

Reactive; recompute when dependent data changes.

Caching

Results not cached; execute each time method is called.

Automatically cache results and recompute only when dependencies change.

Use Case

Suitable for imperative actions and complex logic.

Ideal for deriving values from reactive data.

Performance

May lead to unnecessary re-renders if not optimized.

Optimized for performance with caching mechanism.

Article Tags :