Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/renanbastos93/fastpath
A Golang library for matching REST-like URL path patterns based on ucarion/urlpath
https://github.com/renanbastos93/fastpath
Last synced: 18 days ago
JSON representation
A Golang library for matching REST-like URL path patterns based on ucarion/urlpath
- Host: GitHub
- URL: https://github.com/renanbastos93/fastpath
- Owner: renanbastos93
- License: mit
- Created: 2020-04-29T05:50:13.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-05-11T14:02:05.000Z (over 4 years ago)
- Last Synced: 2024-10-06T16:41:31.894Z (about 1 month ago)
- Language: Go
- Homepage:
- Size: 58.6 KB
- Stars: 4
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
![Go](https://github.com/renanbastos93/fastpath/workflows/Go/badge.svg)
# fastpath
This library was based on [urlpath](https://github.com/ucarion/urlpath) created by [@ucarion](https://github.com/ucarion). It started as a fork, but we've eventually decided to rewrite it from the ground up, based on the original code. All credits for the original library go to @ucarion.## How was it born?
We wanted to come up with a route matching strategy for [Fiber](https://gofiber.io/) because at the time this library was created, it used regex for this purpose. Go's regex is currently very slow compared to other languages, so in order to achieve the best performance, we had to do it our own way.## Usage
```go
package mainimport (
"fmt""github.com/renanbastos93/fastpath"
)func main() {
p := fastpath.New("/api/user/:id")
params, ok := p.Match("/api/user/728342")if !ok {
// not match
return
}
// Matched and have parameters, so will return a slice
fmt.Println(params[0]) // 728342
}
```## Performance
We have compared the performance of `fastpath` with Fiber's regex matcher and the original `urlpath` library.TODO: update benchmarks
```
goos: darwin
goarch: amd64
pkg: github.com/renanbastos93/fastpath
BenchmarkMatch-12 69732186 16.3 ns/op 0 B/op 0 allocs/op
BenchmarkMatchCases//_-_/-12 100000000 10.7 ns/op 0 B/op 0 allocs/op
BenchmarkMatchCases//api/*_-_/api/v1/entity/1-12 75475124 17.2 ns/op 0 B/op 0 allocs/op
BenchmarkMatchCases//api/v1/*/:param3_-_/api/v1/entity/1/2-12 32059923 38.8 ns/op 0 B/op 0 allocs/op
BenchmarkMatchCases//api/v1/:param/*_-_/api/v1/entity/1-12 42632986 25.0 ns/op 0 B/op 0 allocs/op
BenchmarkMatchCases//api/v1/:param/:param2/:nomatch_-_/api/v1/entity/1-12 38993047 32.0 ns/op 0 B/op 0 allocs/op
BenchmarkMatchCases//api/v1/:param/:param2/:param3_-_/api/v1/entity/1/2-12 35778060 33.8 ns/op 0 B/op 0 allocs/op
BenchmarkMatchCases//api/v1/:param/:param2?_-_/api/v1/entity/1-12 48683749 25.2 ns/op 0 B/op 0 allocs/op
BenchmarkMatchCases//api/v1/:param2?_-_/api/v1/-12 79739908 15.2 ns/op 0 B/op 0 allocs/op
BenchmarkMatchCases//api/v1/:param?_-_/api/v1/entity-12 77837749 15.8 ns/op 0 B/op 0 allocs/op
BenchmarkMatchCases//api/v1/const_-_/api/v1/const-12 100000000 10.1 ns/op 0 B/op 0 allocs/op
BenchmarkMatchCases//api/v1/test_-_/api/v1/noMatch-12 138586831 8.59 ns/op 0 B/op 0 allocs/op
BenchmarkMatchCases//config/abc.json_-_/config/abc.json-12 100000000 10.7 ns/op 0 B/op 0 allocs/op
BenchmarkMatchCases//config/noMatch.json_-_/config/abc.json-12 185185951 6.57 ns/op 0 B/op 0 allocs/op
BenchmarkMatchCases/_-_/-12 100000000 10.0 ns/op 0 B/op 0 allocs/op
BenchmarkRegexp-12 1792290 675 ns/op 304 B/op 3 allocs/op
BenchmarkUrlPath-12 7052295 217 ns/op 336 B/op 2 allocs/op
```