Performance

3 snippets across 3 stacks — Python, Regular Expressions, SQL

Also written as or performance

PYPython

Performance Features (3.13+)

PY · Modern Features
syntax
# JIT compiler, free-threaded mode
example
# Python 3.13 introduced:
# 1. Experimental JIT compiler (--enable-experimental-jit)
# 2. Free-threaded mode (no GIL) via python3.13t
# 3. Improved error messages with color

import sys
print(f"Python {sys.version}")

# Check if GIL is disabled (3.13+)
# sys._is_gil_enabled()  # returns False in free-threaded build

Note Python 3.13's free-threaded build removes the GIL experimentally. Most C extensions need updating to work without the GIL. The JIT is opt-in and best for CPU-bound loops.

RXRegular Expressions

Compiling Regex for Reuse

RX · Advanced Techniques
syntax
JS: const re = new RegExp(pattern, flags)
Python: re.compile(pattern, flags)
example
JS:  const dateRe = new RegExp('\\d{4}-\\d{2}-\\d{2}', 'g');
     dateRe.test('2026-04-04');
Py:  date_re = re.compile(r'\d{4}-\d{2}-\d{2}')
     date_re.findall('2026-04-04 and 2025-12-25')
output
JS: true
Py: ['2026-04-04', '2025-12-25']

Note Compiling is beneficial when the same pattern is used repeatedly (e.g., in a loop). Python caches the most recent patterns automatically, but explicit compilation is clearer and avoids cache eviction. In JS, the RegExp constructor requires double-escaping backslashes in string form.

SQLSQL

OR Conditions and Index Usage

SQL · Common Mistakes
syntax
-- Often slow (index may not be used):
WHERE city = 'Seattle' OR state = 'WA'

-- Faster alternative:
SELECT ... WHERE city = 'Seattle'
UNION
SELECT ... WHERE state = 'WA';
example
-- May not use either index effectively:
SELECT * FROM users
WHERE email = '[email protected]'
   OR phone = '2065551234';

-- Better with UNION (each query uses its own index):
SELECT * FROM users WHERE email = '[email protected]'
UNION
SELECT * FROM users WHERE phone = '2065551234';
output
-- UNION lets each branch use its optimal index

Note OR conditions on different columns often prevent the optimizer from using indexes efficiently. Rewriting as UNION (or UNION ALL if you know there are no duplicates) lets each branch use its own index. Check with EXPLAIN to verify.