Skip to content

Instantly share code, notes, and snippets.

View monish-khatri's full-sized avatar
💻
May the code be with you

Monish Khatri monish-khatri

💻
May the code be with you
View GitHub Profile

Optimizing Feature Flags in MySQL: A Bitwise Approach

Understanding Bitwise Operations

Bitwise operations manipulate individual bits in binary numbers. In MySQL, we can use the bitwise AND, OR, XOR, and NOT operators to perform operations on integer values at the bit level. The key advantage of bitwise operations is that they allow us to combine multiple flags into a single integer, saving space and enhancing performance.

Storing Feature Flags

Suppose we have 8 different feature flags, each representing a specific functionality in our application. Traditionally, we might use a separate column for each flag in our database table. However, by leveraging bitwise operations, we can store all 8 flags in a single integer column.

Flag Definitions

let dbNames = 'database_1,database_2';
const dbNamesArray = dbNames.split(",");
// Using Set to remove duplicates and converting back to an array
const uniqueDbNamesArray = [...new Set(dbNamesArray)];
uniqueDbNamesArray.forEach(function(dbName) {
var label = $('label:contains("' + dbName.trim() + ' #")');
var labelFor = label.attr('for');
var inputElement = $('input[value="' + labelFor + '"]');
const footballerStats = {
name: "Lionel Messi",
goals: 819,
assists: 344,
yellowCards: 96,
redCards: 2,
};
// Freeze the footballer's stats to protect them from changes
<?php
// Facing `Warning: Attempt to read property "name" on null`?
$name = $user->name;
/**
* To overcome the above error there are 3 ways in laravel
* 1) if-else condition [Not recommended]
* 2) null safe operator [Available from php8.0]
* 3) optional helper [Best Way]
*/
<?php
// Before
class BeforeTest {
private string $name = 'Monish';
public function getName() {
return $this->name;
}
}
<?php
use App\Models\User;
$updatedUser = User::find(1)->update(['first_name' => 'Monish']); // Output: true/false
$updatedUser = tap(User::find(1), function ($user) {
$user->update(['first_name' => 'Monish']);
}); // Output: User Object with updated value
<?php
// hidden directive in blade file
@hidden('user_id', $user->id)
// Output
<input type="hidden" name="user_id" value="{{$user->id}}">