performance

ruby
class Tag < ApplicationRecord
  has_many :taggings, dependent: :destroy

  scope :top, ->(limit = 20) {
    joins(:taggings)
      .group(Arel.sql("tags.id"))

Memory-Safe “top tags” aggregation with pluck + group

rails activerecord performance
by codesnips 3 tabs
sql
-- Prepared statements basics
-- PostgreSQL syntax
PREPARE get_user (INT) AS
SELECT id, username, email
FROM users
WHERE id = $1;

Query plan caching and prepared statements

postgresql performance query-plans
by Maria Garcia 2 tabs
ruby
class ShardedCounter
  SHARDS = 16
  SNAPSHOT_TTL = 10 # seconds

  def initialize(name, redis: REDIS)
    @name = name

Lock-Free Read Pattern for Hot Counters (Approximate)

rails redis performance
by codesnips 3 tabs
ruby
module Api
  module V1
    class PostsController < BaseController
      def index
        # Eager load author and recent comments with their authors
        posts = Post.published

N+1 prevention with includes and preload

rails activerecord performance
by Alex Kumar 1 tab
ruby
module WriteAmplificationGuard
  extend ActiveSupport::Concern

  def update_if_changed(attrs)
    changed = attrs.each_with_object({}) do |(key, value), acc|
      cast = self.class.type_for_attribute(key.to_s).cast(value)

“Write Amplification” Guard: Only Update Changed Columns

rails activerecord performance
by codesnips 3 tabs
rust
const MAX_SIZE: usize = 1024;

const fn square(n: u32) -> u32 {
    n * n
}

const and const fn for compile-time evaluation

rust const performance
by Marcus Chen 1 tab
ruby
class ProductsController < ApplicationController
  def index
    @products = Product
      .includes(:category)
      .order(created_at: :desc)
      .page(params[:page])

Fragment caching inside Turbo Frames (fast lists)

rails hotwire turbo
by codesnips 4 tabs
typescript
import { onCLS, onINP, onLCP, onFCP, onTTFB, type Metric } from 'web-vitals';

export interface VitalPayload {
  id: string;
  name: Metric['name'];
  value: number;

Web Vitals reporting to an API endpoint

performance observability react
by codesnips 3 tabs
erb
<%# renders a lazy placeholder; real content arrives when scrolled into view %>
<div class="panel-card">
  <h3 class="panel-card__title"><%= title %></h3>

  <%= turbo_frame_tag dom_id(panel, :frame),
        class: "panel-card__body",

Lazy-load heavy panels with IntersectionObserver + Turbo frame

rails hotwire stimulus
by codesnips 3 tabs
ruby
module CollectionCacheKey
  extend ActiveSupport::Concern

  def cache_key_for(scope)
    relation = scope.respond_to?(:all) ? scope.all : scope
    model = relation.klass

Deterministic Cache Keys for Collections

rails caching activerecord
by codesnips 3 tabs
javascript
// k6 Load Test Configuration
// Run: k6 run load-test.js --env BASE_URL=https://api.example.com

import http from 'k6/http';
import { check, sleep, group } from 'k6';
import { Rate, Trend, Counter } from 'k6/metrics';

Load testing APIs with k6 for performance validation

k6 load-testing performance
by Ryan Nakamura 1 tab
typescript
import { createHash } from "crypto";
import { readFileSync } from "fs";

export function sha256(query: string): string {
  return createHash("sha256").update(query, "utf8").digest("hex");
}

GraphQL persisted queries (hash allowlist)

graphql security performance
by codesnips 3 tabs