Skip to content

Commit 133b0e8

Browse files
committed
Update
1 parent 4df748f commit 133b0e8

File tree

5 files changed

+94
-4
lines changed

5 files changed

+94
-4
lines changed

docs/JavaScript/Performance.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Performance
2+
3+
## 生成随机整数
4+
5+
### Math.floor
6+
7+
```js
8+
const i = Math.floor(Math.random() * 100);
9+
```
10+
11+
### 位运算
12+
13+
```js
14+
const i = (Math.random() * 100) | 0;
15+
```
16+
17+
!!! info "提示"
18+
`| 0 `这个技巧是一个性能优化的方案,比使用 `Math.floor()` 要快。适合在在性能关键的场景使用,如:游戏渲染循环、大量数据处理等。
19+
20+
### 更快原因
21+
22+
#### 底层实现差异
23+
24+
1. 位运算 | 0:
25+
1. 直接转换为 CPU 的一条位运算指令
26+
2. 操作数直接在 CPU 寄存器中完成
27+
3. 不需要函数调用开销
28+
2. Math.floor()
29+
1. 需要函数调用开销
30+
2. 需要处理各种边界情况(如 NaN、Infinity 等)
31+
32+
#### 操作步骤对比
33+
34+
```js
35+
// Math.floor() 的过程
36+
let x = 3.7;
37+
Math.floor(x); // 涉及以下步骤:
38+
// 1. 查找 Math 对象
39+
// 2. 调用 floor 函数
40+
// 3. 创建调用栈
41+
// 4. 执行函数逻辑
42+
// 5. 返回结果
43+
44+
// | 0 的过程
45+
let x = 3.7;
46+
x | 0; // 直接进行位运算,一步到位
47+
```
48+
49+
### 限制和注意事项
50+
51+
```js
52+
// 1. 32位整数范围限制
53+
console.log(2147483647 | 0); // 2147483647 (最大值)
54+
console.log(2147483648 | 0); // -2147483648 (溢出)
55+
56+
// 2. 不支持大数处理
57+
console.log(Math.floor(Number.MAX_SAFE_INTEGER)); // 9007199254740991
58+
console.log(Number.MAX_SAFE_INTEGER | 0); // -1 (错误结果)
59+
60+
// 3. 只能用于数字类型
61+
const num = "3.14" | 0; // 可以工作
62+
const invalid = "abc" | 0; // 结果为 0,可能不是期望的行为
63+
```
64+
65+
### 最佳实践
66+
67+
```js
68+
// 推荐的使用场景
69+
function fastIntegerConversion(value: number): number {
70+
// 当确定数值在 32 位整数范围内时使用 | 0
71+
if (value > -2147483648 && value < 2147483647) {
72+
return value | 0;
73+
}
74+
// 其他情况使用 Math.floor
75+
return Math.floor(value);
76+
}
77+
```
78+
79+
在实际开发中,除非在性能关键的场景(如游戏渲染循环、大量数据处理等),否则可读性通常比这种微优化更重要。

docs/JavaScript/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# JavaScript

docs/Python/TiTiler/index.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
---
2+
comments: true
3+
tags:
4+
- GIS
5+
---
6+
17
# TiTiler
28

39
[TiTiler](https://developmentseed.org/titiler/): A modern dynamic tile server built on top of FastAPI and Rasterio/GDAL.

docs/SQL/PostGIS/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ ALTER DATABASE postgis_in_action SET search_path=public,postgis,contrib;
1919
```
2020

2121
!!! info "提示"
22-
虽然不是必需的,但建议在单独的模式(如 postgis)中安装 postgis,这样函数就不会弄乱默认的公共模式。
22+
虽然不是必需的,但建议在单独的模式(如 postgis)中安装 postgis,这样函数就不会弄乱默认的公共模式。
2323

2424
## 查询数据库下所有空间表
2525

docs/SQL/PostgreSQL/index.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
## 修改字段类型
44

5+
### 不带转换
6+
57
```sql
6-
-- 不带转换
78
ALTER TABLE table_name ALTER COLUMN column_name TYPE new_type;
9+
```
10+
11+
## 带转换
812

9-
-- 带转换
13+
```sql
1014
ALTER TABLE table_name ALTER COLUMN column_name TYPE new_type USING column_name::new_type;
11-
```
15+
```

0 commit comments

Comments
 (0)