Techletter #67 | April 6, 2024
Where does the autoincrement in MySQL start 0 or 1?
Have you ever wondered what the count of a column of the first row in MySQL which is set to autoincrement? Recently, I got this question while performing data migration.
I needed the autoincrement number to be set to a number after the migration. But, I was not sure what would be the starting number of autoincrement, Is it 1 or 0? By default, it starts at 1.
You can change the autoincrement value to a particular number, with the following command:
ALTER TABLE table_name AUTO_INCREMENT = 100;
MySQL allows only one autoincrement column per table.
Shallow copy vs deep copy
Shallow Copy: In a shallow copy, a new object is created, but the properties of the new object share the same references as those of the original object. This means that changes made to the properties of the new object will affect the original object as well.
Shallow copies are created using methods like assignment operator, spread syntax, Array.from()
, and Object.assign()
const originalObject = {
a: 1,
b: {
c: 2
}
};
const shallowCopy = { ...originalObject };
// Modifying the property of the shallow copy
shallowCopy.b.c = 3;
console.log(originalObject); // { a: 1, b: { c: 3 } }
console.log(shallowCopy); // { a: 1, b: { c: 3 } }
Deep Copy: On the other hand, a deep copy involves creating a new object where all the members of the old object are copied to a separate memory location. This results in both objects being independent of each other, so modifications to one object do not affect the other. To create a deep copy in JavaScript, JSON.parse()
and JSON.stringify()
methods can be used.
const originalObject = {
a: 1,
b: {
c: 2
}
};
const deepCopy = JSON.parse(JSON.stringify(originalObject));
// Modifying the property of the deep copy
deepCopy.b.c = 3;
console.log(originalObject); // { a: 1, b: { c: 2 } }
console.log(deepCopy); // { a: 1, b: { c: 3 } }
What is hoisting and why do we need it?
Hoisting is one of the important concepts of JavaScript. Without it, it would be a great difficulty in writing and maintaining large applications.
Hoisting is a mechanism that moves declarations to the top of their respective scopes before the code execution. This behavior allows us to use functions, variables, and classes before they are declared in the code.
printHello(); // "hello"
function printHello() {
console.log("hello");
}
MySQL vs PostgreSQL
Recently, I benchmarked a few query executions with both MySQL and PostgreSQL. I found out that PostgreSQL is better in performance compared with MySQL. There are a lof of other dependencies that needs to kept in mind when making such tests for your product.
Context matters a lot.
PostgreSQL may be better than MySQL, but there comes other costs if you want to migrate. Some of them are listed below.
- Rewrite table structures.
- Migrate data
- Test Foreign Keys
- Test all the APIs
All of them require a lot of time. So, it totally depends on the business requirements and affordability.
function.length in JavaScript
The function.length
property in JavaScript indicates the number of arguments expected by a function, excluding rest parameters and only counting parameters before the first one with a default value.
function greet(name, age) {
console.log(`Hello ${name}, you are ${age} years old.`);
}
console.log(greet.length); // Output: 2
The function.length ignores rest parameters and parameters with a default value.
function greet(name, age = 10, ...subjects) {
console.log(`perform function`);
}
console.log(greet.length); // Output: 1