Skip to content

Instantly share code, notes, and snippets.

@zaru
Created January 28, 2026 13:11
Show Gist options
  • Select an option

  • Save zaru/cffd0718ab7abaa4a9907e159d24ab9b to your computer and use it in GitHub Desktop.

Select an option

Save zaru/cffd0718ab7abaa4a9907e159d24ab9b to your computer and use it in GitHub Desktop.
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// JSONファイルを読み込む関数
function loadTestData(filename) {
const filePath = path.join(__dirname, 'data', filename);
const content = fs.readFileSync(filePath, 'utf-8');
return JSON.parse(content);
}
// ベンチマーク実行関数
function benchmark(name, fn, data, iterations = 1000) {
// ウォームアップ
for (let i = 0; i < 10; i++) {
fn(data);
}
// 計測開始
const start = performance.now();
for (let i = 0; i < iterations; i++) {
fn(data);
}
const end = performance.now();
const total = end - start;
const average = total / iterations;
return {
name,
total: total.toFixed(2),
average: average.toFixed(4),
iterations
};
}
// structuredClone関数
function cloneWithStructuredClone(data) {
return structuredClone(data);
}
// コマンドライン引数のパース
const args = process.argv.slice(2);
const dataSize = args[0]; // 'small', 'medium', 'large', 'xlarge'
// 使用方法の表示
if (!dataSize) {
console.log('使用方法: node benchmark-structured-clone.js <data-size>');
console.log('');
console.log('data-size:');
console.log(' small - 小サイズ (10要素, ~100KB)');
console.log(' medium - 中サイズ (100要素, ~1MB)');
console.log(' large - 大サイズ (1000要素, ~10MB)');
console.log(' xlarge - 特大サイズ (5000要素, ~50MB)');
console.log('');
console.log('例: node benchmark-structured-clone.js small');
process.exit(1);
}
// データサイズの定義
const sizeConfig = {
'small': { name: '小サイズ', file: 'small.json', iterations: 100000 },
'medium': { name: '中サイズ', file: 'medium.json', iterations: 10000 },
'large': { name: '大サイズ', file: 'large.json', iterations: 1000 },
'xlarge': { name: '特大サイズ', file: 'xlarge.json', iterations: 1000 }
};
// データサイズの検証
if (!sizeConfig[dataSize]) {
console.error(`エラー: 不正なデータサイズ '${dataSize}'`);
console.error('利用可能なデータサイズ: small, medium, large, xlarge');
process.exit(1);
}
const testCase = sizeConfig[dataSize];
// ベンチマーク実行
const data = loadTestData(testCase.file);
const result = benchmark(
'structuredClone()',
cloneWithStructuredClone,
data,
testCase.iterations
);
// 結果表示
console.log(`合計: ${result.total}ms 平均: ${result.average}ms`);
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// テストデータ生成関数
function generateTestData(size) {
const data = {
id: 1,
name: 'Test User',
email: 'test@example.com',
active: true,
timestamp: new Date().toISOString(),
nested: {
level1: {
level2: {
level3: {
value: 'deeply nested'
}
}
}
},
tags: ['tag1', 'tag2', 'tag3'],
items: []
};
// サイズに応じて配列を拡張
for (let i = 0; i < size; i++) {
data.items.push({
id: i,
title: `Item ${i}`,
description: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit',
price: Math.random() * 1000,
inStock: Math.random() > 0.5,
metadata: {
created: new Date().toISOString(),
updated: new Date().toISOString(),
tags: Array(5).fill(null).map((_, j) => `tag-${i}-${j}`)
}
});
}
return data;
}
// データディレクトリを作成
const dataDir = path.join(__dirname, 'data');
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir);
}
// テストケース定義
const testCases = [
{ name: 'small', size: 10, description: '小サイズ' },
{ name: 'medium', size: 100, description: '中サイズ' },
{ name: 'large', size: 1000, description: '大サイズ' },
{ name: 'xlarge', size: 5000, description: '特大サイズ' }
];
console.log('テストデータを生成中...\n');
testCases.forEach(testCase => {
console.log(`${testCase.description} (${testCase.name}.json) を生成中...`);
const data = generateTestData(testCase.size);
const filePath = path.join(dataDir, `${testCase.name}.json`);
fs.writeFileSync(filePath, JSON.stringify(data, null, 2));
const stats = fs.statSync(filePath);
const sizeKB = (stats.size / 1024).toFixed(2);
console.log(` → 配列要素数: ${testCase.size}, ファイルサイズ: ${sizeKB} KB`);
});
console.log('\n✓ すべてのテストデータを生成しました');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment