codeflash/docs/optimizing-with-codeflash/one-function.mdx
Mohamed Ashraf 81e665dde4 docs: fix Java documentation gaps across 5 pages
Add Java to supported languages in how-codeflash-works, add auth and
GitHub App steps to java-installation, add Java tab to codeflash-all
tip, reorder trace-and-optimize Java examples, and clarify Java class
method syntax in one-function.

Closes CF-1090

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-08 16:28:19 +00:00

99 lines
2.8 KiB
Text

---
title: "Optimize a Single Function"
description: "Target and optimize individual functions for maximum performance gains"
icon: "bullseye"
sidebarTitle: "Optimize Single Function"
keywords:
[
"function optimization",
"single function",
"class methods",
"performance",
"targeted optimization",
"javascript",
"typescript",
"python",
"java",
]
---
Codeflash is essentially a function optimizer. When asked to optimize a function, Codeflash will analyze the function,
any helper functions it calls, and the imports it uses. It will then generate multiple new versions of the function and its helper functions, that are
optimized for efficiency and performance while being functionally equivalent to the original function.
Codeflash tests if a function is optimized by running the function and comparing the output of the new function to
the original function. If the outputs match and the runtime of the new function is smaller, then the function is considered optimized.
Codeflash works best on pure functions that don't have side effects. It can also optimize functions with side effects, but
your mileage may vary.
## How to optimize a function
To optimize a function, run the following command in your project:
<Tabs>
<Tab title="Python">
```bash
codeflash --file path/to/your/file.py --function function_name
```
</Tab>
<Tab title="JavaScript">
```bash
codeflash --file path/to/your/file.js --function functionName
```
</Tab>
<Tab title="TypeScript">
```bash
codeflash --file path/to/your/file.ts --function functionName
```
</Tab>
<Tab title="Java">
```bash
codeflash --file src/main/java/com/example/Utils.java --function methodName
```
</Tab>
</Tabs>
If you have installed the GitHub App to your repository, the above command will open a pull request with the optimized function.
If you want to optimize a function locally, add a `--no-pr` argument:
<Tabs>
<Tab title="Python">
```bash
codeflash --file path/to/your/file.py --function function_name --no-pr
```
</Tab>
<Tab title="JavaScript / TypeScript">
```bash
codeflash --file path/to/your/file.ts --function functionName --no-pr
```
</Tab>
<Tab title="Java">
```bash
codeflash --file src/main/java/com/example/Utils.java --function methodName --no-pr
```
</Tab>
</Tabs>
### Optimizing class methods
To optimize a method `methodName` in a class `ClassName`:
<Tabs>
<Tab title="Python">
```bash
codeflash --file path/to/your/file.py --function ClassName.method_name
```
</Tab>
<Tab title="JavaScript / TypeScript">
```bash
codeflash --file path/to/your/file.ts --function ClassName.methodName
```
</Tab>
<Tab title="Java">
```bash
codeflash --file src/main/java/com/example/Utils.java --function methodName
```
In Java, use just the method name — no `ClassName.` prefix is needed. Codeflash discovers the method by name within the specified file.
</Tab>
</Tabs>